iOS pop使用代理傳值


假如oneViewController頁面push到OtherViewController頁面,然后你想從OtherViewController頁面pop到oneViewController頁面的時候需要傳值,這時可以使用代理。

 

從OtherViewController中.h文件中定義代理,並設置代理屬性,代碼如下

#import <UIKit/UIKit.h>
@protocol OneDelegate
- (void)returnName:(NSString *)name;
@end

@interface OtherViewController : UIViewController
@property (nonatomic, retain) id <OtherDelegate> delegate;
@end

 然后在.m文件中,設置Other控制器的代理是One控制器,這里我們在自定義的pop事件中設置代理

- (void)pop {

    OneViewController *VC = [[OneViewController alloc]init];
    self.delegate = VC;
    [self.delegate returnName:@"名字"];
    [self.navigationController popViewControllerAnimated:YES];
}

 

 

然后在oneViewController中,遵循代理,並且實現代理所必須的方法,同時把接收到的值,保存在自己的屬性中

 

@interface OneViewController () <OtherDelegate>
@property (nonatomic, copy) NSString *name;
@end

//所需要實現的代理方法 - (void)returnName:(NSString *)name { self.name = name; NSLog(@"代理收到名字 %@",self.name); }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM