[IOS development-26] implements forward and reverse value transfer between the UINavigationController views using protocol proxies. ioscontroller

Source: Internet
Author: User

[IOS development-26] implements forward and reverse value transfer between the UINavigationController views using protocol proxies. ioscontroller
Lab description

(1) Forward pass value: for example, the local value in Class A must be passed to Class B, that is, we first declare A Class B object in Class A (of course the Class B header file needs to be imported ), then pass A value in Class A to A value in Class B (so you need to prepare A variable in Class B to accept it, that is, use @ property and @ synthesize as the entire variable ).


(2) reverse transfer of values: for example, you need to pass the values in Class B to Class. We first write a protocol in Class B. There is a method in the protocol that can contain parameters. This parameter is the value to be passed (this protocol can also be written separately, is not necessarily written in Class B), and Class B follows this protocol, creates A delegate variable using this protocol, and then assigns this delegate variable to Class, then implement the methods in the Protocol in Class.


(3) Of course, the highlights are:

  • Although Class B complies with the Protocol but does not implement the methods in the Protocol, it only transmits A value to this method and then enables Class A to implement this method, of course, at the same time, Class A will accept the value passed by Class B to this method, and this value is the value we need to pass, so it is worth passing.
  • Clever Use of the delegate variable. First, the delegate variable in Class B is used to pass A value to the method, and then the delegate variable is assigned to Class A in Class, therefore, when implementing the Protocol method, Class A actually obtains the value passed by Class B using the delegate variable.

References

(1) For more information about why sec1.delegate1 = self exists in ViewController. m, see xxxx. delagate = self;


(2) For Delegation variables, if you do not understand them, refer to Objective-C Protocol Introduction + delegation implementation and the Protocol and delegation in iOS.


(3) If you do not understand the value transfer mode of the entire delegate agent, refer to the protocol developed in iOS: pass value by proxy.


Lab Purpose

(1) implement reverse value transfer: Click ViewController to enter SecondViewController (three buttons are available), click any button, return to ViewController, and change the title of this button to the title on the navigation bar. Then


(2) implement forward value transfer: click "SecondViewController" to change the text color of the button with the same title on the navigation bar to red (that is, the one that was clicked in the previous three buttons ).


Therefore, we need a navigation controller, which has two view controllers: ViewController and SecondViewController. ViewController is the Root View Controller. Of course, we need to initialize the navigation controller in AppDelegate. m, set ViewController as the Root View Controller, and set self. window. rootViewController as the navigation Controller so that the navigation controller can be displayed. This part of code is omitted here. We mainly look at the code in the two view controllers.


Lab code

In SecondViewController. h:

# Import <UIKit/UIKit. h> // create a protocol with the passed value @ protocol sendValue <NSObject>-(void) sendBtnTitle :( NSString *) btnTitle; @ end // make this SecondViewController class comply with this Protocol @ interface SecondViewController: UIViewController <sendValue> // create a delegate variable using this protocol, class Object can use this delegate variable to call methods in the protocol, similar to an attribute, such as SecondViewController. [self. delegate1 sendBtnTitle: text1]; // This delegate variable is set to which class, which is equivalent to which class to become a proxy. Because people delegate it to you, can you be a proxy? In this example, sec1.delegate1 = self @ property (nonatomic, assign) id <sendValue> delegate1; @ property (nonatomic, assign) NSString * titleText; @ end

In SecondViewController. m:

# Import "SecondViewController. h "@ interface SecondViewController () @ end @ implementation SecondViewController @ synthesize delegate1; @ synthesize titleText;-(void) viewDidLoad {// use for to create three buttons and obtain the title respectively, add a click event and make an if judgment. if the titleText value passed on the ViewController page is the same as the title value of the button, then the title of this button becomes red for (int I = 1; I <= 3; I ++) {UIButton * btn = [[UIButton alloc] init]; btn. frame = CGRectMake (38, 60 + 40 * I, 300, 30); btn. backgroundColor = [UIColor purpleColor]; [btn setTitle: [NSString stringWithFormat: @ "% I", I] forState: UIControlStateNormal]; if ([titleText is1_tostring: btn. currentTitle]) {btn. selected = YES;} [btn setTitleColor: [UIColor redColor] forState: UIControlStateSelected]; [btn addTarget: self action: @ selector (backTo :) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: btn];} [super viewDidLoad]; // Do any additional setup after loading the view .} // because we need to obtain the title of the current button, we need to pass the button object and add a parameter-(void) backTo :( UIButton *) btn {// use the currentTitle attribute to obtain the title NSString * text1 = btn of the current button. currentTitle; // use the delegate variable to send a message to the method in the protocol for execution, but this execution is in ViewController. the implementation in m is actually equivalent to the notification method to start [self. delegate1 sendBtnTitle: text1]; [self. navigationController popViewControllerAnimated: NO];} @ end

In ViewController. h:

# Import <UIKit/UIKit. h> # import "SecondViewController. h" // follow the <sendValue> Protocol @ interface ViewController: UIViewController <sendValue> @ end

In ViewController. m:

# Import "ViewController. h "# import" SecondViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {// Add a button. Click it to go to the SecondViewController view and click UIButton * btn1 = [UIButton buttonWithType: UIButtonTypeRoundedRect]; btn1.frame = CGRectMake (38, 80,300, 30); btn1.backgroundColor = [UIColor whiteColor]; [self. view addSubview: btn1]; [btn1 setTitle: @ "jump to secondviewcontroller" forState: UIControlStateNormal]; [btn1 addTarget: self action: @ selector (jumpTo :) forControlEvents: UIControlEventTouchUpInside] [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib .} -(void) jumpTo :( NSString *) barTitle {// Initialize an object first, this is because the delegate variable in this object is given to our current class SecondViewController * sec1 = [[SecondViewController alloc] init]; // This is the key, you need to assign the previous delegate variable to the current class (that is, the ViewController View Controller). Since the delegate is accepted as a proxy, You need to implement the method in the Protocol, because this method is not implemented in SecondViewController, it finds ViewController as a proxy to help it implement it. In fact, this method is used to transmit values. Of course, this method requires parameters, otherwise, pass the fart value sec1.delegate1 = self; // pass the title of the current navigation bar to a variable of SecondViewController, which is the forward pass value sec1.titleText = self. navigationItem. title; [self. navigationController pushViewController: sec1 animated: NO];} // reverse transfer value: Assign the value passed by the Protocol Method to the title-(void) sendBtnTitle :( NSString *) of the current navigation bar *) btnTitle {self. navigationItem. title = btnTitle;} @ end


Lab result screenshot



Ios development: How to click a button to jump to a new interface ,~~~

1. You can use the navigation controller stack. Use the current view controller as the rootViewController. You need to create it in the code that creates the current controller.
UIViewController * vc1 = [[UIViewControlelr alloc] init];
UINavigationController * navController = [[UINavigationController alloc] initWithRootViewController: vc1];
[Vc1 release];
[Window addSubView: navController. view];
[NavController release];

PushViewController can be used to navigate other views only when the current controller is in the navigation controller stack.
Navigate to the New View Controller:
UIViewController * vc2 = [[ViewController alloc] init];
[Self. navigationController pushViewController: vc2 animated: YES];
[Vc2 release];
2. Modal View
UIViewController * vc2 = [[ViewController alloc] init];

[Self presentModalViewController: controller animated: YES];
[Vc2 release];

3. overwrite the current view with the new view
If this method is used, we recommend that you create a controller swithController that can maintain the interaction between controllers. In this controller, You can implement view switching between different controllers.
@ Inertface SwitchViewController: UIViewController
@ Property (retain) UIViewController * vc1
@ Property (retain) UIViewController * vc2;

-(Void) showVC1;
-(Void) showVC2;
@ End

@ Implementation SwitchViewController
@ Synthesize vc1, vc2;

-(Void) showVC1 {
If (vc2 ){
[Vc2.view removeFromSuperView];
}

[Self. view addSubView: vc1.view];
}
@ End
 
Uinavigationcontroller does not have a root controller to display things. I have created an AddressViewController, h and m file, and

1. Create and use a UINavigationControllerUINavigationController * aNav = [[UINavigationController alloc] init]; then add a view, otherwise, UIViewController * aView = [[UIView alloc] initWithNibName: (* xib file name *); [aNav pushViewController: aView animated: NO]; // do not animate the first view in the navigation bar 2. Set the left and right buttons in the navigation bar: As I said, setting the navigation bar button is not to set the navigation bar itself, but to the View Controller that is being navigated at that time, for example, we set aView. Set the title: aView. title = @ "title"; // configure a button. Here is the code UIBarButtonItem * callModalViewButton = [[UIBarButtonItem alloc] initWithTitle: @ "Scripture" style: UIBarButtonItemStyleBordered target: self action: @ selector (callModalList)]; self. navigationItem. leftBarButtonItem = callModalViewButton; [callModalViewButton release]; // because the local view will retain it, we can see it through release, which is quite simple. 3. Other common methods and attributes: local view. navigationItem. leftBarButtonItem // local view of the left sidebar. navigationItem. rightBarButtonItem // local view of the right sidebar. navigationItem. backBarButtonItem // The local view of the Back-bar project. navigationItem. hidesBackButton // hide the back button (YES or NO). In the viewWillAppear: Method of the view, add: [self. tableView reloadData];

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.