how to pass a string value from one view controlle

2020-06-28 01:10发布

hi i am new to objective c. i have a login view controller with .h, .m, .xib files. and after successful login i need to go to the second page.

The scenario is this, i am accessing web services. to authenticate the user, i send the username and password to the web service and in return i get a string value. based on the results of the string value i need to display a second screen. Please help

6条回答
Deceive 欺骗
2楼-- · 2020-06-28 01:37

You can't display values in nib files.

Get a reference to the Standard user defaults and store the string in there. Then you'll have access to the value wherever you want.

查看更多
叛逆
3楼-- · 2020-06-28 01:41

OPNe way is to create properties in the app delegate. Each controller can access the appdelegate with:

[[UIApplication sharedApplication] delegate]
查看更多
女痞
4楼-- · 2020-06-28 01:51
NSString *str1 = @"Apple";
NSString *str2 = @"Orange";

if([str1 isEqualToString: str2]) {

}else{

}

it will help you

查看更多
Lonely孤独者°
5楼-- · 2020-06-28 01:52

You can create a class to store some shared data and create an instance of it in your application delegate. in such way you can use this shared data in any class of your app.

查看更多
疯言疯语
6楼-- · 2020-06-28 01:54

Is this your questions; when you get your response from the server, you need to make a view controller and put that string into it?

Try something like this in your first view controller

// Get the string from the server
NSString *string = [get string from server];

// Create the second controller
SecondViewController *controller = [[SecondViewController alloc] initWithNibName:@"nibname" bundle:nil];

// Set the text property of a label in the controller
controller.myLabel.text=string;

// Add the view to the window so we can see it
[[[[UIApplication sharedApplication] delegate] window] addSubview:controller.view];

This will create a new controller, set a string in it and display it in the window.

You will need to make a xib file that contains a UILabel and attach it to a propety called myLabel in the second controller i.e.

@interface SecondViewController {
  UILabel *myLabel;
}

@property (nonatomic, retain) IBOutlet UILabel *myLabel;

@end

Hope this helps,

Sam

查看更多
时光不老,我们不散
7楼-- · 2020-06-28 01:55

If you only need to pass just a few values you can make them parameters to the init method. For example:

-(id)initWithUserName:(NSString *)name andPassword:(NSString *)password {
    self = [super init];
    if (nil == self) {
        return nil;
    }

    // display or store login info somewhere
    [someLabel setText:name];

    return self
}

Otherwise if you have a lot of values you want to use in the next view go with Morion's advice and make a separate class.

查看更多
登录 后发表回答