[分享]iOS開發-導航控制器下UIViewController之間的6種傳值方式

1.代理(delegate)傳值 ---- 顧名思義就是委託別人辦事,就是當一件事情發生後,本身不處理,讓別人來處理。
實質就是:好比右AB兩個頁面,A想要傳值給B ,就只要先在A中獲得B的指針,而後將想要傳的值賦給B,以後跳轉async

代碼以下:post

A.h  
@protocol HMTShowViewControllerDelegate <NSObject>  
  
@optional  
-(void)showViewGiveValue:(NSString *)text;  
@end  
  
@interface HMTShowViewController : UIViewController <UITextFieldDelegate>  
  
@property (nonatomic,copy)NSString * text;  
// 定義一個代理  
  
@property (nonatomic,assign)id<HMTShowViewControllerDelegate> delegate;  
  
@end  
  
  
A.m  
-(void)popPerView:(UIBarButtonItem *)barButton{  
  
    // 在頁面跳轉前將參數傳出去  
    if ([self.delegate respondsToSelector:@selector(showViewGiveValue:)]) {  
        [self.delegate showViewGiveValue:_showTextField.text];  
    }  
    [self.navigationController popViewControllerAnimated:YES];  
      
}  
  
  
  
B.h  
@interface HMTInputViewController : UIViewController <HMTShowViewControllerDelegate>  
  
B.m  
-(void)pushNextViewControl:(UIBarButtonItem *)button{  
  
    HMTShowViewController * showVC = [[HMTShowViewController alloc]init];  
    showVC.text = _textField.text;  
    // 將代理對象設置成B  
    showVC.delegate = self;  
    [self.navigationController pushViewController:showVC animated:YES];   
  
}
// B實現協議裏面的方法  
-(void)showViewGiveValue:(NSString *)text{  
  
    _inputLabel.text = text;  
}

2.單例傳值 ------- 若是頁面之間相隔不少,要進行傳值,將值保存到第三方,將第三方設置爲單例模式atom

代碼以下:spa

static HMTInputViewController * shareRootViewController = nil;  
  
@implementation HMTInputViewController  
  
+(HMTInputViewController *)sharedController{  
    @synchronized(self){  
        if(shareRootViewController == nil){  
            shareRootViewController = [[self alloc] init] ;  
        }  
    }  
    return shareRootViewController;  
}

要將HMTInputViewController中的一個UITextField編輯的內容傳到HMTShowViewController中的UILabel
_showLabel.text = [HMTInputViewController sharedController].textField.text;.net


3.Target-Action傳值
實質就是:A頁面要給B頁面傳值,A就提供接口出去,抓A到B內部來,A間接調用本身內部方法(至關於,A把本身內部須要操做的方法,傳到B內來,到B內部進行賦值,這樣就不存在訪問不到各自的局部實例變量)代理

@property (nonatomic,assign)id target;
@property (nonatomic,assign)SEL action;
[self.traget performSelector:self.action withObject:nil];(是否須要傳參數本身定,最多2個)指針

代碼以下:code

A.h  
@interface HMTShowViewController : UIViewController <UITextFieldDelegate>  
  
@property (nonatomic,assign) id traget;  
@property (nonatomic,assign) SEL action;  
  
@end  
  
A.m  
-(void)popPerView:(UIBarButtonItem *)barButton{  
  
    [self.traget performSelector:self.action withObject:_showTextField.text];  
    [self.navigationController popViewControllerAnimated:YES];  
  
}  
  
B.m  
-(void)pushNextViewControl:(UIBarButtonItem *)button{  
  
    HMTShowViewController * showVC = [[HMTShowViewController alloc]init];  
    showVC.traget = self;  
    showVC.action = @selector(changeLabelText:);  
    [self.navigationController pushViewController:showVC animated:YES];  
  
}  
  
-(void)changeLabelText:(NSString *)text{  
      
    _inputLabel.text = text;  
      
}

4.屬性/方法傳值orm

A - pushViewController - B 在A視圖中有B視圖的對象 ,可直接操做server

-(void)didClickRightButtonItemAction  
{  
    InputViewController * inputVC = [[InputViewController alloc] init];  
  
    [self.navigationController pushViewController:inputVC animated:YES];  
  
}

B - popViewControllerAnimated - A 由於A推出B,A只是隱藏了,退到棧底去了,在B返回A,A的viewDidLoad並不會再次調用

-(void)didClickLeftButtonItemAction  
{  
    ShowViewController * showView = (ShowViewController *)[self.navigationController.viewControllers objectAtIndex:0];  
      
    [self.navigationController popViewControllerAnimated:YES];  
}

5.block傳值(A-view 傳值給 B-view)
@釋放 Block_release(_doTransferMsg);

A.h  
@property (nonatomic,copy) void(^doTransferMsg)(NSString * msg);  
  
A.m  
-(void)popPerView:(UIBarButtonItem *)barButton{  
  
    if (_doTransferMsg) {  
        _doTransferMsg(_showTextField.text);  
          
    }  
      
    [self.navigationController popViewControllerAnimated:YES];  
}  
  
  
B.m  
-(void)pushNextViewControl:(UIBarButtonItem *)button{  
  
    HMTShowViewController * showVC = [[HMTShowViewController alloc]init];  
      
    // block傳值  
    [showVC setDoTransferMsg:^(NSString * msg) {  
        dispatch_async(dispatch_get_main_queue(), ^{  
            _inputLabel.text = msg;        });  
    }];  
      
    [self.navigationController pushViewController:showVC animated:YES];   
  
}

1344323584_7609.png


6.通知傳值(同上,也是A傳給B)

A.m  
-(void)popPerView:(UIBarButtonItem *)barButton{  
  
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeLabelTextNotification" object:_showTextField.text];  
      
    [self.navigationController popViewControllerAnimated:YES];  
}  
  
B.m  
-(void)viewDidLoad  
{  
    [super viewDidLoad];  
      
    // 通知中心,發送一條消息傳值--------其中name千萬不要寫錯了,會出如今3個地方  
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showViewGiveValue:) name:@"changeLabelTextNotification" object:nil];  
      
}  
  
-(void)showViewGiveValue:(NSNotification *)notification{  
    // 取出notification的object  
    id text = notification.object;  
    _inputLabel.text = text;  
}  
  
-(void)dealloc{  
    // 消息發送完,要移除掉  
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeLabelTextNotification" object:nil];  
    [super dealloc];  
      
}

分享來源:
http://blog.csdn.net/hmt20130...

相關文章
相關標籤/搜索