Get ViewController created by Storyboard in IOS development, and develop storyboard in ios

Source: Internet
Author: User

Get ViewController created by Storyboard in IOS development, and develop storyboard in ios

The previous two blogs are about screen adaptation, that is, relative layout. This blog will learn how to switch between views. To switch between views, we can use code or storyboard to establish the relationship between view controllers. When code switching is required, code is used to redirect between views. The following describes how to switch between views through NavigationController. The following describes how to combine the code and storyboard as before.

The following describes the functions and effects to be implemented: the first interface opened by the App is the login page, which consists of the user name and password and a login button. When the login succeeds, it will push to the second page. The second page has three buttons, A, B, and C. Click different buttons and jump to the corresponding View.

1. First, use our storyboard to drag out the control we want to use and add a NavigationController. The following figure shows how to add NavigatinController to the main view. Figure 2 shows the final result.

 

2. because the login status is used to determine whether the following View appears, switching between the main View and the View with the, B, and C buttons cannot be implemented using the storyboard, we need to associate code for implementation. The following are the modules to jump to after successful login:

3. the drag-and-drop operation ends at this point. To realize the relationship between our business logic and the associated view, you can find the view with three buttons when associating the view, we need to set the StoryboardID of the view

 

4. Write our code below. TextField is used above. We need to handle the keyboard reclaim event. Therefore, our ViewController must comply with the UITextFiledDelegate Protocol to implement keyboard-related methods.

(1) comply with the UITextFieldDelegate Protocol

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

(2) perform callback registration in ViewController. m and implement the corresponding methods in the Protocol. The Code is as follows:

1234567891011121314 -(BOOL) textFieldShouldReturn:(UITextField *)textField{    [self.userName resignFirstResponder];    [self.password resignFirstResponder];    return YES;} - (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 handle the callback event when the login button is clicked. First, obtain the TextFiled value in the callback method, whether to switch between pages based on the value. When switching pages, we need to associate the relationship between the two pages.

12345678910111213141516171819 - (IBAction)tapButton:(id)sender {         NSString *username = self.userName.text;    NSString *password = self.password.text;         if ([username isEqualToString:@"admin"] && [password isEqualToString:@"admin"])    {        // Obtain storyboard: Use bundle to obtain our storyboard according to the storyboard name,        UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];                 // The storyboard obtains the view to be switched Based on the storyBoardID of myView.        UIViewController *myView = [story instantiateViewControllerWithIdentifier:@"myView"];                 // Push the navigationController to the view we want to push        [self.navigationController pushViewController:myView animated:YES];             } }

Code Description: three views are required to associate two views.

1. Get storyboard: Get the bundle through the bundle name, and get our storyboard through the storyborad name;

2. Obtain the View where the storyboardID is myView on the storyboard;

3. Execute the myView obtained from the current View;

At this point, the main code is compiled. Let's take a look at the running effect:

 

 

Let's simply talk about this. Later I learned new content and updated the Blog content. I welcome criticism and correction.


How to obtain the viewcontroller instance in storyboard during iOS development

The essence of an object is: feature encapsulation and State maintenance. To put it bluntly, code and data are involved. Although the instance is created using the same nib, they are not objects pointed to by the same pointer. If you want to distinguish between them, you can add attributes such as instanceName. You can specify this instanceName as a different value during creation.
A. instanceName = @ "";
Next. instanceName = @ "Next ";

I don't need to talk about how to judge if you want an instance.

How can I obtain the specified viewController from a viewController when I use storyboard for iOS development?

UITableViewController * startingTableController = [self. storyboardinstantiateViewControllerWithIdentifier: @ "UITableViewController"];

[Self. view addSubview: startingTableController. view];

// InstantiateViewControllerWithIdentifier: The Identifier (Identifier) that defines the attributes of the form during design );
// [Self. storyboardinstantiateViewControllerWithIdentifier: @ "UITableViewController"]; it returns a controller object
// [Self. view addSubview: startingTableController. view]; Add the Controller view to the main window view. The disadvantage of this is that there is a blank area, which seems to be
The error message returned when the startingTableController taskbar is missing.

Set [self. view addSubview: startingTableController. view];
For

NSArray * viewController = [NSArray arrayWithObject: startingTableController];
[Self. pageViewController setViewControllers: viewControllerdirection: UIPageViewControllerNavigationDirectionForward animated: NO completion: NULL];
[Self addChildViewController: self. pageViewController];
[Self. view addSubview: self. pageViewController. view];
Can solve the blank Problem
 

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.