开发者

How Do I Pass a NSSString from RootViewController to another View?

I'm coding an iphone app which takes user inputs from First View (RootViewController) and then it needs to pass on the inputs to the "results" View Controller which is another view which uses the inputs to query a server, pa开发者_C百科rse JSON string and displays results in the UITableView. I'm stuck at how to "send" these string (from user inputs on RootViewController) to the 2nd ViewController...Any idea?

Thx in advance

Stephane


There are three ways to do this, depending on how the views are set up.

First, you can use NSNotificationCenter to post a notification of the string. The other view would register as an observer for the notification and can gather the information when it is posted.

Secondly, if the second view controller is presented by the first, i.e. you alloc/init the VC and present it with a navigation controller, you can create a property in the second VC and set it from your root. In the header of the second VC, you would create the following:

NSString *someString;

and

@property (nonatomic, retain) NSString *someString;

then @synthesize someString; in the implementation file. Doing this lets you set the value before the view is presented.

Lastly, if the views are not related, as in the second VC is not presented by the root, you would create an IBOutlet from the root to the second VC. Assuming you have the attribute set up like in the last solution, you would call something along the lines of self.secondVC.someString = myStringToPass;

Hopefully one of those helps

Edit: Realized I had commented out the link to NSNotificationCenter....oops


In the second view controller, create an instance of NSString to receive the value and set it when you are going to show this controller, for example in the tableView:didSelectRowAtIndexPath: method.
RootViewController.h

@interface RootViewController : UITableViewController
{
    NSString *stringToPass;
}

@property (nonatomic, retain) NSString *stringToPass;

@end

RootViewController.m

#import "SecondViewController.h"

@implementation RootViewController

@synthesize stringToPass;

// Other code goes here...

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // for example first cell of first section
    if (indexPath.section == 0 && indexPath.row == 0)
    {
        SecondViewController *second = [[SecondViewController alloc] initWithStyle:UITableViewStyleGrouped];
        // here you pass the string
        second.receivedString = self.stringToPass;
        [self presentModalViewController:second animated:YES];
        [second release];
    }
}
@end

SecondViewController.h

@interface SecondViewController : UITableViewController
{
    NSString *receivedString;
}

@property (nonatomic, retain) NSString *receivedString;

@end

SecondViewController.m

@implementation SecondViewController

@synthesize receivedString;

// methods to use the string goes here

@end

I haven't tested this code... I've written remembering it :)


Subclass the second view control and write a custom init method.

-(id)initWithMyCustomValueString:(NSString*)string;

And pass your data to it.


Make sure you create an iVar or property on your secondViewController to read the data from.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜