How to send an NSString to another view controller

2020-02-16 04:42发布

I have an IBAction that when triggered calls another method in a different view controller ( APICallsViewController). I'm looking to also send that method an NSString (message)

here's my IBAction with the push to the APICallsViewController and also the NSString message. My question might be how do I grab the contents of that NSString in the other view controller's method.

thanks for any help

-(IBAction) someMethod{
        APICallsViewController *apiViewController = [[APICallsViewController alloc] init];
        [self.navigationController pushViewController:apiViewController animated:YES]; 

        NSString *message = [NSString stringWithFormat:@"Check out %@", nameLb.text];

        [apiViewController apiGraphUserCheckins];

        [apiViewController release];

}

4条回答
相关推荐>>
2楼-- · 2020-02-16 04:58

do this code in APICallsViewController.h

@interface APICallsViewController : UIViewController{
   NSString *strMessage;

}
@property(nonatomic,retain) NSString *strMessage;

@end

APICallsViewController.m

 @synthesize strMessage;

- (void)viewDidLoad {
Nslog(@"%@",strMessage);

}


-(IBAction) someMethod{

    APICallsViewController *apiViewController = [[APICallsViewController alloc] init];
    [self.navigationController pushViewController:apiViewController animated:YES]; 

    NSString *message = [NSString stringWithFormat:@"Check out %@", nameLb.text];
    apiViewController.strMessage=message;
    [apiViewController apiGraphUserCheckins];

    [apiViewController release];

}
查看更多
来,给爷笑一个
3楼-- · 2020-02-16 05:00

Declare a property in SecondViewController. Then you can get string in SecondViewController in 2ways.

  1. In FirstViewController someMethod after creating object for secondviewController you can assign the value directly

    second.string2 = [NSString stringWithFormat:@"%@", [textField text]];

  2. Create a method in SecondViewController and assign through it.

查看更多
兄弟一词,经得起流年.
4楼-- · 2020-02-16 05:01

Why do you want to pass the String programmatically, instead of declaring a function parameter? You could change the Function to something like

- (void) apiGraphUserCheckins:(NSString *)message;

call it with

[apiViewController apiGraphUserCheckins:[NSString stringWithFormat:@"Check out %@", nameLb.text]]; 
查看更多
混吃等死
5楼-- · 2020-02-16 05:07

Declare a string in the viewcontroller to where you have to pass the string.And in the view from which you have to pass the string,in your case,set like apiViewController.stringintheotherview=message;

The string in your APICallsViewController must be synthesized

 NSString *message = [NSString stringWithFormat:@"Check out %@", nameLb.text];
         APICallsViewController *apiViewController = [[APICallsViewController alloc] init];
 apiViewController.stringintheotherview=message;
            [self.navigationController pushViewController:apiViewController animated:YES]; 
查看更多
登录 后发表回答