开发者

How to pass variables between 2 view conrollers

I have 2 view controllers now, And it both got tableviews. When I choose a row in the second tableview (Using didSelectRowAtIndexPath),

and I want to pass the Information I got in the second View to the first View, I tried to use delegate&protocol, but don't know why, It didn't work.

And I tried to use class method inside the first class, when I got variable in sencond View, Call the class method inside the first class. The variable successfully pass to first View,

but when I want to set the Lable's text, it still failed..

Can somebody teach me how to do? thanks!


My protocol&delegate.

This is the second view.

@protocol CategoriesViewControllerDelegate;
@interface CategoriesViewController : UIViewController {
    TableViewNewAppDelegate *appDelegate;
    id <CategoriesViewControllerDelegate> delegate;

}
@property (nonatomic, assign) id <CategoriesViewControllerDelegate> delegate;
@end

@protocol CategoriesViewControllerDelegate <NSObject>

-(void)backstring:(NSString *)String;

@end

In the .m file , synthesize it

@implementation CategoriesViewController
@synthesize delegate;

didSelectRowAtindexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    CategoryData *CateObj = [appDelegate.CateArray objectAtIndex:indexPath.row];
    NSString *Strings = [NSString stringWithString:CateObj.CateTitle];
    [delegate backstring:Strings];

    [self.parentViewController dismissModalViewControllerAnimated:YES];

}

In the first view controller .h file.

#import "CategoriesViewContro开发者_开发知识库ller.h"
@interface DataController : UIViewController <CategoriesViewControllerDelegate>{

.m file

-(void)backstring:(NSString *)String {
    NSLog(@"%@",String);
    jCateField.text = String;
}

This is how I do my protocol+delegate. Are there something wrong?

btw, I created a Class method in the first view controller, and use the Class method in the sencond view controller, it succesfully pass variable to first view controller.

But the problem is, I can't set my Label's text inside my Class method, even calling Instance method to set text. Is there any way to solve this problem?


The code you provided seems to be correct. In your case you must set :

@property (nonatomic, assign) id <CategoriesViewControllerDelegate> delegate;

correctly to point to the first view controller which conforms to the protocol you defined :

#import "CategoriesViewController.h"
@interface DataController : UIViewController <CategoriesViewControllerDelegate>{

So it seems that you pushed a CategoriesViewController onto a first DataController, you probably missed to do so just before.

// self is the first view controller
// [myCategoriesViewController setDelegate:self]; old fashion
myCategoriesViewController.delegate = self;
[self presentModalViewController:myCategoriesViewController animated:YES];

This can probably solve your issue. Hope this helps. Also consider let the first controller dismiss the second. Here is a link to Apple's documentation.


You could just pass the information straight on to your second view controller;

SecondViewController.h

@interface SecondViewController
{
    Information *info;
}

@property (nonatomic, retain) Information *info;

@end

SecondViewController.m

@implementation SecondViewController

@synthesize info;
...

@end

And in your didSelectRowAtIndexPath method;

SecondViewController *controller = [[SecondViewController alloc] initWithNibNamed:@"SecondViewController" bundle:nil];
[controller setInfo:YOUR_INFO_OBJECT];

[self.navigationController pushViewController:controller animated:YES];
[controller release];


Import second view controller header file in the first view controller implementation file. Import first view controller header file in second view controller header file. Create the property (text/label/whatever) in the first view controller. Create the property of first view controller in the second view controller. Create the second view controller instance, set the first view controller property to what you need, push controller to the navigation controller. In the second view controller change whatever you want in the first view controller. Instance methods allowed. Do not forget to release first view controller.

Delegate pattern works in that way too.


View controllers are objects. Objects can have methods that can be called from other objects, and they can have instance variables. ("Delegate" is just a fancy term for this.)

There's no inherent reason why passing data between your view controllers should be hard or complicated, so long as the caller has the address of the callee. (And whether or not a given VC has an XIB is irrelevant.)

It sounds like your real problem is not knowing what to do with the data once it's been passed to the callee.

Stupid question: Is "jCateField" actually connected to the label you want to change, or is it nil? If you created the label from code (since you don't have an XIB), you will need to have stored the created label's address into "jCateField" during the view creation.


Can you post the code for as to ho you are displaying the contents when you come back to 1 st view controller.As here if the log gives you proper value then the issue is with the connection (if taken through iboutlet) or with addsubview . Do you get nil in label or no value (label is hidden).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜