天天看點

IOS Storyboard中使用Segue傳值

故事闆中,VIEW1與VIEW2有一條SEGUE連線。點選VIEW1中的按鈕跳轉至VIEW2,并且從VIEW1中傳遞值給VIEW2。

實作:

VIEW1.m

添加下面的事件方法,該方法在視圖跳轉時被觸發。

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{   

    if([segue.identifier isEqualToString:@"goView2"]) //"goView2"是SEGUE連線的辨別

    {        

        id theSegue = segue.destinationViewController;

        [theSegue setValue:@"這裡是要傳遞的值" forKey:@"strTtile"];

    }           

}

VIEW2.h

定義一個屬性來接受SEGUE傳遞過來的值: @property(nonatomic,weak)NSString * strTtile;

VIEW2.m

@synthesize strTtile;

......

- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    NSLog(@"接收到的值為: %@",  strTtile);

}