Value transfer from the IOS navigation controller to the ios navigation Controller

Source: Internet
Author: User

Value transfer from the IOS navigation controller to the ios navigation Controller

UINavigationController is the main tool used to build layered applications. It uses stacks to implement views. Any type of view controller can be put into the stack. When designing the navigation controller, you must specify the root view, that is, the first view that the user sees. The Root View Controller is the first View Controller pushed to the stack by the navigation controller. When you view the next attempt, A New View Controller is added to the stack, and the view controlled by the Controller is displayed to the user. We can use the navigation buttons to operate the layered application and use it to control the push or release of the view. When multiple controllers jump to each other, it is certainly designed to pass the value, next we will study how to transmit values between controllers:

1. We use a simple example to transfer values between controllers. click the button on the first page to jump to the second page, click the button on the second page to return the title on the first page and change it to the title of the button. At the same time, when the second page is returned, the last clicked button is displayed in red.


2. First, define a controller with a push button.

@implementation JRViewController

-(void) viewDidLoad {
    [super viewDidLoad];
    
    // Set the title to item1 by default
    self.title = @ "item1";

    // 1 set view
    [self setSubviews];

}

#pragma mark-Set subview
-(void) setSubviews {
    
    // Set the background color
    self.view.backgroundColor = [UIColor greenColor];
    
    // Add button
    UIButton * button = [[UIButton alloc] initWithFrame: CGRectMake (0, 0, 140, 45)];
    button.backgroundColor = [UIColor redColor];
    [button setTitle: @ "push" forState: UIControlStateNormal];
    button.center = self.view.center;
    [button addTarget: self action: @selector (pushAction) forControlEvents: UIControlEventTouchUpInside];
    [self.view addSubview: button];
   
}
2. We now define the second controller with four buttons



@implementation SecondViewController

-(void) viewDidLoad {
    [super viewDidLoad];
   
    [self setSubviews];

}
#pragma mark-Set subview
-(void) setSubviews {
    
    // Set the background color
    self.view.backgroundColor = [UIColor blueColor];
    
   
    CGPoint point = self.view.center;
    point.y = point.y-100;
    
    
    for (int i = 0; i <4; i ++) {
       // Add button
        UIButton * button = [[UIButton alloc] initWithFrame: CGRectMake (0, 0, 140, 45)];
        
        // Reset the new center point coordinates
        CGPoint newPoint = point;
        newPoint.y + = i * 55;
        button.center = newPoint;
        
        
        // Set button attribute event
        button.backgroundColor = [UIColor grayColor];
        NSString * title = [NSString stringWithFormat: @ "item% d", i + 1];
        [button setTitle: title forState: UIControlStateNormal];
        [button addTarget: self action: @selector (popAction :) forControlEvents: UIControlEventTouchUpInside];
        [self.view addSubview: button];
        
        // Set the corresponding button title to red based on the passed title
        if ([title isEqualToString: self.buttonTitle]) {
            [button setTitleColor: [UIColor redColor] forState: UIControlStateNormal];
        }
        
        
    }
  
}
3. Below we will consider the page jump. When we jump from the first controller to the second controller, we need to turn the button of the second controller red, so we need to change the title of the first one. Pass it in, here we pass the value by adding an attribute in the second controller, as shown below:

@interface SecondViewController: UIViewController

/ ** Button title * /
@property (nonatomic, copy) NSString * buttonTitle;

@end
When jumping, just assign a value to the second controller

#pragma mark-EventAction

-(void) pushAction {

    SecondViewController * sec = [[SecondViewController alloc] init];
    
    // Set up a proxy to pass the value back
    sec.delegate = self;
    
    sec.buttonTitle = self.title;
    
    [self.navigationController pushViewController: sec animated: YES];

}
4. We have now passed the title of the first controller to the second controller. Below we consider how to control the title change of the first controller when the second page is clicked. To change the value of the first controller, so let ’s consider implementing it through a proxy

// Set protocol
@protocol ChangeTitleDelegate <NSObject>

-(void) changeTitle: (NSString *) title;

@end
@property (nonatomic, weak) id <ChangeTitleDelegate> delegate;
Click method
#pragma mark-popAction
-(void) popAction: (UIButton *) button {
    
    
    [self.delegate changeTitle: button.titleLabel.text];
    
    [self.navigationController popViewControllerAnimated: YES];
    
}
Just implement the method in the first controller

#pragma mark-proxy method
-(void) changeTitle: (NSString *) title {
    self.title = title;

}
 

Author: Jerry education
Source: http://www.cnblogs.com/jerehedu/
Copyright statement: The copyright of this article belongs to Yantai Jerry Education Technology Co., Ltd. and Blog Park. Welcome to reprint, but this paragraph statement must be retained without the author ’s consent, and the original text link is given in an obvious position on the article page, otherwise the right to pursue legal responsibility is reserved .
technical consulting:

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.