iOS development, through the storyboard can directly switch the scene, that is, jump between different viewcontroller, in the process of jumping will automatically call the Prepareforsegue method, we in this method can be directly to the target scene set the value to pass in; Here's an introduction to the small demo that uses KVC and normal attribute methods to pass values.
For example, there are two view controllers, A and B,
A The view controller has a button, drag the button to the event on the B controller (using show),
A in the process of switching needs to give B a number value, B received after the value is displayed;
The class of a binding is, viewcontroller,b is bound to be bviewcontroller
Using the storyboard value, the default is to trigger
-(void) Prepareforsegue: ( Uistoryboardsegue *) Segue Sender: (ID) sender
In this method, the Segue parameter contains 3 important attributes:
are: Controller identifiers,
The source controller, which is a controller in the
The target controller, which is the B controller.
The corresponding properties are as follows:
@property (nonatomic, readonly) nsstring *identifier; @property (nonatomic, readonly) ID Sourceviewcontroller; @property (Nonatomic, readonly) ID Destinationviewcontroller;
Sender is the event source that triggers this scene switch, which is the button
The code is as follows:
A controller corresponding code:
viewcontroller.h// testpreparesegue//// Created by YB on 15/2/10.// Copyright (c) 2015/HTTP Blog.csdn.net/yangbingbinga. All rights reserved.//#import <UIKit/UIKit.h> @interface viewcontroller:uiviewcontroller@end
viewcontroller.m// testpreparesegue//// Created by YB on 15/2/10.// Copyright (c) 2015/HTTP Blog.csdn.net/yangbingbinga. All rights reserved.//#import "ViewController.h" #import "BViewController.h" @interface Viewcontroller () @ End@implementation viewcontroller-(void) viewdidload { [super Viewdidload]; } #pragma makr-scene toggle, suitable for storyboard drag and drop-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender{ NSLog (@ "The type of sender object triggering the scene toggle is:%@", [Sender class]); #pragma mark-method one, using KVC to B is the target scene Uiviewcontroller *destinationcontroller=[segue Destinationviewcontroller]; [Destinationcontroller setvalue:@ "119" forkey:@ "number"]; #pragma mark-Method 2, use the attribute value to import the related class. h // Bviewcontroller *bcontroller=[segue destinationviewcontroller];// [email protected]; #pragma mark-Method 3, coercion type conversion } @end
b Controller corresponding Code:
bviewcontroller.h// testpreparesegue//// Created by Yang bin on 15/2/10.// Copyright (c) 2015/HTTP Blog.csdn.net/yangbingbinga. All rights reserved.//#import <UIKit/UIKit.h> @interface bviewcontroller:uiviewcontroller@property (Strong, nonatomic) Nsvalue *number;//receives the value from a controller @end
bviewcontroller.m// testpreparesegue//// Created by Yang bin on 15/2/10.// Copyright (c) 2015/HTTP Blog.csdn.net/yangbingbinga. All rights reserved.//#import "BViewController.h" @interface Bviewcontroller () @property (weak, nonatomic) Iboutlet UIButton *buttononb, @end @implementation bviewcontroller-(void) viewdidload { [super viewdidload]; #pragma mark- Print received from a value of number NSLog (@ "%@", _number); [_buttononb settitle:[nsstring stringwithformat:@ received a value from a:%@ ", _number] forstate:uicontrolstatenormal]; } @end
Run, click the button, you can jump to B and a to B successfully passed the value of a nsvalue type number,
When passing a value, use the KVC or attribute to pass the value
The final effect is as follows:
You can see the B controller and get the value from a.
Precautions:
1. Do not directly drag the switch event to the Navigationcontroller of B on the button, the event source, or it will cause a crash
2. According to the above, a group of Viewcontrollers common one navigationcontroller best, can add title after deletion navigation
3. Sometimes it may be necessary to compare the type of sender to determine the source of the jump; we can compare sender, use Isequalto comparison, determine which sender triggered the switching scenario
Original address: Http://blog.csdn.net/yangbingbinga
Ios-prepareforsegue Scene switch KVC value explanation