How to switch KVC values in ios-prepareForSegue scenarios

Source: Internet
Author: User

How to switch KVC values in ios-prepareForSegue scenarios

During iOS development, storyboard can be used to directly switch the scenario, that is, jump between different viewcontrollers. During the jump process, the prepareForSegue method is automatically called, in this method, we can directly set the value to be passed in for the target scenario. Next we will introduce a small Demo that uses KVC and common attribute methods to pass values.

For example, there are two view controllers, A and B,

A View Controller has A button on it. Drag the button event to Controller B (use show ),

A needs to give B a number value during the switching process, and B will display it after receiving the value;

A is bound to ViewController, and B is bound to BViewController.

If you use storyboard to pass the value, it is triggered by default.

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

In this method, the segue parameter contains three important attributes:

Are: controller identifier,

Source Controller, in which is A controller

The target controller is the Controller B in the middle.

The corresponding attributes 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 switching in this scenario, that is, the button

The Code is as follows:

The code corresponding to Controller:

 

/// ViewController. h // TestPrepareSegue // Created by yb on 15/2/10. // Copyright (c) 2015 http://blog.csdn.net/yangbingbinga. All rights reserved. // # import
 
  
@ 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-scenario switching, suitable for switching through storyboard Drag and Drop-(void) prepareForSegue :( UIStoryboardSegue *) segue sender :( id) sender {NSLog (@ the sender object type that triggers the scenario switch is: % @, [sender class]); # pragma mark-Method 1: Use KVC to pass the value UIViewController * destinationController = [segue destinationViewController] to B in the target scenario; [destinationController setValue: @ 119 forKey: @ number]; # pragma mark-Method 2: import related classes to use attribute to pass values. h // BViewController * bController = [segue destinationViewController]; // bController. number = @ 188; # pragma mark-method 3, forced type conversion} @ end
The corresponding code of controller B:

 

 

/// BViewController. h // TestPrepareSegue // Created by Yang Bin on 15/2/10. // Copyright (c) 2015 http://blog.csdn.net/yangbingbinga. All rights reserved. // # import
 
  
@ Interface BViewController: UIViewController @ property (strong, nonatomic) NSValue * number; // receives A 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 the received value number NSLog (@ % @, _ number) from A; [_ buttonOnB setTitle: [NSString stringWithFormat: @ receives the value from: % @, _ number] forState: UIControlStateNormal];} @ end

Run it. click the button to jump to B and A successfully passes in A value of the NSValue type to B,

 

When passing a value, use KVC or attribute to pass the value

The final result is as follows:



 

We can see that controller B successfully obtains the value from Controller.

Note:

1. Do not drag the switch event to B's NavigationController directly on the button, that is, the event source. Otherwise, the system will crash.

2. Based on the above, it is best for a group of ViewControllers to share a NavigationController. You can add the title and delete the Navigation.

3. Sometimes we may need to compare the sender type to determine the source of the jump. We can compare the sender and use isverto to determine which sender triggers the switch scenario.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.