iOS Development acquisition storyboard created by Viewcontroller

Source: Internet
Author: User
Tags call back

The previous two blogs are all about screen adaptations and relative layouts, and this blog will learn to switch between views. Switching between views we can use code completion or use storyboard to establish the relationship between the various view controllers. When you need to switch between code, you use code to jump between views. The following things we will mainly introduce to the Navigationcontroller to make the switch between views. Here is the same as before the code and storyboard together to learn.

Here is what we want to achieve the function and effect: The app opens the first interface is the landing page, landing page by the user name and password and a landing button. When the login success will push to the second page, the second page has three buttons, a, B and C, click on a different button and then jump to the corresponding view.

1. First we use our storyboard to drag and drop the control we want to use, and then add a navigationcontroller, the following figure one is to add Navigatincontroller to our main view, figure two is the final effect

2. Because the login status to determine whether the following view appears, so the main view and the views with a,b,c three buttons between the switch can not be implemented with storyboard, we want to use Code association implementation. Here is the module to jump after the successful landing:

3. The above drag and drop work to the end, the following to achieve the relationship between our business logic and the associated view, in order to correlate the view with three buttons to find the view, we need to set the view of the Storyboardid, into

4. Below to write our code, we use the TextField, we need to handle the keyboard recycling events, so our viewcontroller to comply with the Uitextfileddelegate protocol, the implementation of the keyboard method

(1) Compliance with Uitextfielddelegate Agreement

1234 #import <UIKit/UIKit.h>@interface ViewController : UIViewController<UITextFieldDelegate>@end

? (2) in the VIEWCONTROLLER.M in the callback registration and implementation of the corresponding method in the protocol, the Code is as follows:

1234567891011121314 -(BOOL) textFieldShouldReturn:(UITextField *)textField{    [self.userName resignFirstResponder];    [self.password resignFirstResponder];    returnYES;}- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    self.userName.delegate = self;    self.password.delegate = self;}

5. After processing the keyboard, we should deal with the event that we call back when the login button is clicked, first get the value of textfiled in the callback method, and the condition of the value to realize whether to switch between pages. We have to correlate the relationships in two pages when the page is toggled.

12345678910111213141516171819 - (IBAction)tapButton:(id)sender {        NSString *username = self.userName.text;    NSString *password = self.password.text;        if([username isEqualToString:@"admin"] && [password isEqualToString:@"admin"])    {        //获取storyboard: 通过bundle根据storyboard的名字来获取我们的storyboard,        UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main"bundle:[NSBundle mainBundle]];                //由storyboard根据myView的storyBoardID来获取我们要切换的视图        UIViewController *myView = [story instantiateViewControllerWithIdentifier:@"myView"];                //由navigationController推向我们要推向的view        [self.navigationController pushViewController:myView animated:YES];            }}

? Code Description: Associated two view requires three

1. Get Storyboard: Get bundles through the name of the bundle and get our storyboard through Storyborad's name;

2. Access to Storyboardid by storyboard is a view of myview;

3. Implementation of the MyView that the current view pushes us to obtain;

Now that our main code is written, let's look at how it works:

?

First of all, let's just say these things, and later learn new content in the Update blog content, welcome criticism.

iOS Development acquisition storyboard created by Viewcontroller

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.