在两个视图控制器之间传递字符串

时间:2010-05-28 10:56:20

标签: ios objective-c iphone string uiviewcontroller

我的问题很简单,但作为初学者,我迷路了:D

我必须查看控制器:

查看一个调用视图2,如下所示:

self.FacebookTypeRequest =@"favoris"; 
FaceBookViewController *viewcontrol = [[FaceBookViewController alloc]initWithNibName:@"FaceBookViewController"  bundle:[NSBundle mainBundle]];
viewcontrol.title = @"FaceBook";
[self.navigationController pushViewController:viewcontrol animated:YES];
[viewcontrol release];

如何将我的字符串facebookTypeRequest发送到我的视图控制器2?

1 个答案:

答案 0 :(得分:2)

在.h文件中的第二个视图控制器(FaceBookViewController)上创建一个属性:

@interface FaceBookViewController {
    ...
    NSString *facebookTypeRequest;
    ...
}

@property (nonatomic, copy) NSString *facebookTypeRequest;

并在.m文件中添加

@implementation FaceBookViewController

    @synthesize facebookTypeReqeust;

并记得加入你的dealloc方法

- (void) dealloc {
    [facebookTypeRequest release];
    // release other properties here as well
    [super dealloc];
}

然后,你可以这样设置:

self.FacebookTypeRequest = @"favoris";
FaceBookViewController *viewcontrol = [[FaceBookViewController alloc] initWithNibName:@"FaceBookViewController" bundle:nil];
viewcontrol.title = @"FaceBook";
viewcontrol.facebookTypeRequest = self.FacebookTypeRequest; //!< This is the line :)
[self.navigationController pushViewController:viewcontrol animated:YES];
[viewcontrol release];

现在,在你的FaceBookViewController中你有facebookTypeRequest。

希望有所帮助。

注意:使用强制字母来启动属性的名称通常是不好的做法,即self.FaceboookTypeReqeust应该是self.facebookTypeRequest。