Value passing in the basic iOS control UINavigationController and iosui Control

Source: Internet
Author: User

Value passing in the basic iOS control UINavigationController and iosui Control

In the Basic iOS control UINavigationController, the value is transmitted by proxy, forward, and reverse.

 

# Import <UIKit/UIKit. h> // declare a protocol @ protocol SendValue <NSObject> // define a method-(void) sendBtnTitle :( NSString *) title; @ end @ interface FirstViewController: UIViewController // defines the proxy @ property (nonatomic, assign) id <SendValue> delegate; // creates a forward value passing attribute @ property (nonatomic, copy) NSString * currentTitle; @ end

 

 

 

-(Void) didButton {NSArray * btntitles = [NSArray arrayWithObjects: @ "press one", @ "press two", @ "press three", nil]; for (int I = 0; I <btntitles. count; I ++) {UIButton * btn = [UIButton buttonWithType: UIButtonTypeSystem]; // change the font color of the button [btn setTitleColor: [UIColor blackColor] forState: UIControlStateNormal]; [btn setTitle: [btntitles objectAtIndex: I] forState: UIControlStateNormal];
// If the button title is the same as the _ currentTitle in the property, that is, the title of the navigation bar in the root page is the same if ([_ currentTitle is1_tostring: btn. currentTitle]) {
// Enable the selected status btn. selected = YES; btn. tintColor = [UIColor whiteColor];} [btn setTitleColor: [UIColor redColor] forState: UIControlStateSelected]; [btn addTarget: self action: @ selector (titlebtnClicked :) forControlEvents: Listener]; btn. frame = CGRectMake (80,140 + 60 * I, self. view. frame. size. width-160, 40); btn. backgroundColor = [UIColor whiteColor]; [self. view addSubview: btn] ;}}- (void) titlebtnClicked :( UIButton *) btn {NSString * title = btn. currentTitle;

// Determine whether the proxy has the sendBtnTitle: This function

If ([_ delegate respondsToSelector: @ selector (sendBtnTitle :)]) {

// The proxy executes its own sendBtnTitle function. The parameter is title.

[_ Delegate sendBtnTitle: title];

}

    NSLog(@"%@",title);    [self.navigationController popViewControllerAnimated:YES];    }
# Import <UIKit/UIKit. h> # define C (a, B, c, d) [UIColor colorWithRed: a/255.0 green: B/255.0 blue: c/255.0 alpha: d/255.0] # define IMG (name) [UIImage imageNamed: name] # import "FirstViewController. h "// mount the Protocol @ interface rootViewController: UIViewController <SendValue> @ end
-(Void) btnClicked {FirstViewController * first = [[FirstViewController alloc] init]; // set the navigationItem of the current page. the title is passed in. // The forward value is first. currentTitle = self. navigationItem. title; // specify the proxy as the first pointer of the current rootViewController class. delegate = self; [self. navigationController pushViewController: first animated: YES];} // method for implementing Protocol definition-(void) sendBtnTitle :( NSString *) title {self. navigationItem. title = title ;}

Page value:

Forward data transfer uses attribute data transfer;

Use a proxy to transmit Reverse values;


Attribute pass value: if you want to pass A value from page A to page B, declare the attribute on page B and assign A value to the attribute of page B in the jump event on page;
Returning from the previous page does not execute the loadView and viewDidLoad methods of the previous page, but executes the viewWillAppear method, because the loadView and viewDidLoad methods are used to load the view to the memory, when returning from the next page, the previous page has been loaded to the memory, so you do not need to load it again, so do not execute the loadView and ViewDidLoad methods;

 

Pass value by proxy: Same as the proxy design mode, follow the four steps of setting proxy and following the protocol,

/*

1. Define the Protocol (BaoMuProtocol)

2. Protocol-compliant class (Nurse)

3. Define the class (Mother) for proxy)

4. Establish a relationship (Mother defines a member variable of the proxy type)

*/

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.