Domanda

in my app i have multiple classes :D, the thing is i need to access a variable from class a in class b. For this purpose i use synthesize, i declare my variable in my .h between interface brackets, then declare a property of the variable wich is a NSString. Then i use synthesize :). But when i access it in class b i have a NSString equal to nil :§. When i breakpoint to check if the NSString get filled it works the NSLog shows the right string in the classe a, but in class b i got nil :§. Secondly i would like to have your opinion : it is better to use a global like NSUserDefaults or a old school c global declaration or this "synthesize" way of doing it ? If anyone would like to help me please, thank you a lot :).

//polymorphViewController.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "LCYDataBackedTableView.h"
#import "RootViewController.h"
#import "LCYLockSettingsViewController.h"
#import "LockScreenAppDelegate.h"
#import "MyWebViewController.h"
#import "MemberViewController.h"
#import "SharedViewController.h"

#import "UploadedViewController.h"
@interface PolymorphViewController : LCYDataBackedTableView{
    NSString *secondId;
}

@property (readwrite,nonatomic) NSString *secondId;
@end

//in class a .h

@interface firstViewController : UITableViewController <UISearchBarDelegate, UISearchDisplayDelegate>{

IBOutlet UITableView * newsTable;
UISearchBar *searchBar;
UISearchDisplayController *searchDisplayController;
...
NSString  *idNumber;
}

@property (readwrite, retain) NSString *idNumber;
- (void)launchIt:(NSString *)typeofdata sharedOrUploaded:(BOOL)smogogo;
@end

//in class a .m
@implementation firstViewController
@synthesize idNumber
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1]; 

idNumber = [[stories objectAtIndex: storyIndex] objectForKey: @"sizing_id"];

PolymorphViewController *polymorph = [[PolymorphViewController alloc]init];
[[self navigationController] pushViewController:polymorph animated:YES];
[polymorph viewDidLoad];
}

//in class B .m
-(void)dataByChoice: (NSString *)command{

self.title = @"Test";
myWebView = [[UIWebView alloc] initWithFrame:self.view.frame];
[self.view addSubview:myWebView];


NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *defaults = [prefs stringForKey:@"myKey"];
NSString *defaults2 = [prefs stringForKey:@"mySecondKey"];

NSString *username = defaults;
NSString *password = defaults2;

firstViewController *WC = [[firstViewController alloc]initWithNibName:nil    bundle:nil];
NSLog(WC.idNumber); <-- equals nil :(
NSString *idName = WC.idNumber;
NSString *partialUri = [@"http://www.google.com" stringByAppendingString: idName];
NSString *finalUri = [partialUri stringByAppendingString: command];
NSURL *url = [NSURL URLWithString:finalUri];
È stato utile?

Soluzione

your class be should look like this .h file @interface Class_B : UIViewController

        @property (copy,nonatomic) NSString *someValue;

    @end

.m file

    @implementation Class_B

    @synthesize someValue = _someValue;
    - (void) viewDidLoad {

        _someValue = @"This is my come value";


    }


    @end

and calling class (Class_A) it should be like this

- (void) viewDidLoad {

    Class_B *cls_b = [[Class_B alloc] initWithNibName:@"Class_B_nib" bundle:nil];

    NSLog(@"Some value echo :%@",cls_b.someValue);

}

Altri suggerimenti

Use NSUserDefaults when you need to store locally data among sessions of the app, I would not use it just to share variables among classes.

@synthesize directive is used to generate accessors methods for properties (nowadays, unless you want to change the name of setters and getters, you don't even need it) declared with @property directive.

If you just need to access the property from another object then a property declared in the interface file (.h) is perfect. If you need to share a property across the app, I am thinking about the singleton pattern but there are definitely many ways to do it.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top