iOS ViewController之間傳遞資料

來源:互聯網
上載者:User

標籤:

翻譯自stackoverflow的問題

前向傳遞資料:

例如你有ViewControllerA和ViewControllerB,從ViewcontrollerA向ViewcontrollerB傳遞一個BOOL變數:

(1)在ViewControllerB.h建立一個屬性

@property (nonatomic, assign) BOOL *isSomethingEnabled;

(2)在ViewcontrollerA中引入標頭檔

#import "ViewControllerB.h"

 當點擊didSelectRowAtIndex或者IBAction的時候可以用以下代碼設定ViewControllerB的屬性

ViewControllerB *viewControllerB = [[ViewControllerB alloc] initWithNib:@"ViewControllerB" bundle:nil];viewControllerB.isSomethingEnabled = YES;[self pushViewController:viewControllerB animated:YES];

用Segue前向傳遞資料:

用以下方法傳遞資料:

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

(1)在ViewControllerB.h建立一個屬性

@property (nonatomic, assign) BOOL *isSomethingEnabled;

(2)在ViewcontrollerA中引入標頭檔

#import "ViewControllerB.h"

(3)建立一個從ViewControllerA到ViewControllerB的segue,不妨取名為showDetailSegue

(4)實現方法:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{    if([segue.identifier isEqualToString:@"showDetailSegue"]){        ViewControllerB *controller = (ViewControllerB *)segue.destinationViewController;        controller.isSomethingEnabled = YES;    }}

如果你的view是嵌套在navigation controller裡

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{    if([segue.identifier isEqualToString:@"showDetailSegue"]){        UINavigationController *navController = (UINavigationController *)segue.destinationViewController;        ViewControllerB *controller = (ViewControllerB *)navController.topViewController;        controller.isSomethingEnabled = YES;    }}

後向傳遞資料:

ViewControllerA裡面點擊某些按鈕,跳轉到ViewControllerB,裡面的事件執行完成後,又要跳回ViewControllerA,並且把一些資料傳遞迴ViewControllerA。這裡需要用到代理模式。

(1)在ViewControllerB.h中,#import下,@interface上聲明一個協議

@protocol ViewControllerBDelegate <NSObject>- (void)addItemViewController:(ViewControllerB *)controller didFinishEnteringItem:(NSString *)item;@end

(2)在ViewControllerB.h設定一個delegate屬性

@property (nonatomic, weak) id <ViewControllerBDelegate> delegate;

(3)在ViewControllerB中,當我們退出這個ViewController的時候

NSString *itemToPassBack = @"Pass this value back to ViewControllerA";[self.delegate addItemViewController:self didFinishEnteringItem:itemToPassBack];

(4)在ViewControllerA.h中,引入標頭檔並實現協議

#import "ViewControllerB.h"@interface ViewControllerA : UIViewController <ViewControllerBDelegate>

(5)在ViewControllerA.m中,

- (void)addItemViewController:(ViewControllerB *)controller didFinishEnteringItem:(NSString *)item{    NSLog(@"This was returned from ViewControllerB %@",item);}

(6)設定代理

ViewControllerB *viewControllerB = [[ViewControllerB alloc] initWithNib:@"ViewControllerB" bundle:nil];viewControllerB.delegate = self[[self navigationController] pushViewController:viewControllerB animated:YES];

 

 

 

  

  

iOS ViewController之間傳遞資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.