Ios_16_ Controller switching _modal_ code mode

Source: Internet
Author: User

Eventually:



Main.storyboard



BeyondViewController.h

  beyondviewcontroller.h//  16_ Controller switching mode 1_modal_ by code////  Created by beyond on 14-7-30.//  Copyright (c) 2014 Com.beyond. All rights reserved.//#import <UIKit/UIKit.h> @interface beyondviewcontroller:uiviewcontroller// Hint welcome who who @property (weak, nonatomic) Iboutlet UILabel *welcomelabel; @property (weak, nonatomic) Iboutlet UIButton * WANTLOGINBTN; @property (weak, nonatomic) Iboutlet UIButton *wantlogoutbtn;//Click the login button on the Beyondviewcontroller interface, Switch to BeyondLoginViewController.h to enter password account login-(ibaction) Wantlogin: (UIButton *) sender;-(ibaction) Wantlogout: (UIButton *) sender; @end


Beyondviewcontroller.m

beyondviewcontroller.m//16_ Controller Switching mode 1_modal_ by code////Created by Beyond on 14-7-30.//Copyright (c) 2014 Com.bey Ond. All rights reserved./* Controller switch 3 ways: 1,modal (modal dialog, the new controller from the bottom up, cover the rear controller) (more can be see Luo Yunbin of the < gallstone in-windows environment, 32-bit assembly language programming > Chapter 5.4 dialog box) through the code to implement the switch through the storyboard implementation of the switch 2,push through the Uinavigationcontroller management of the stack implementation from the right to the left to expand, pop up a new controller (at the top of the stack), involving the main content : The title of the pass-through navigation bar of the parameter customizing the pre-jump verification, typically such as the client check 3,uitabbarcontroller before the login jump is managed in a parallel way is the management sub-view control System 4, custom container, similar to drawer effect (right slide, popup left sidebar) */#import "BeyondViewController.h" #import "BeyondLoginViewController.h" @interface Beyondviewcontroller () <BeyondLoginViewControllerDelegate,UIActionSheetDelegate> @end @implementation beyondviewcontroller-(void) viewdidload{[super Viewdidload];} Click the login button on the Beyondviewcontroller interface, switch to BeyondLoginViewController.h to enter password account login-(ibaction) Wantlogin: (UIButton *) Sender {//want to switch to the Beyondloginviewcontroller controller through code in a modal way, you must create an instance object, the coupling is too strong ~ Beyondloginviewcontroller *loginviewctr L= [[Beyondloginviewcontroller alloc]init]; The proxy setting for Loginviewctrl is the current controller because, in the next control (Loginviewctrl), after the user has entered the user name and password, the agent's Dosomethingwithusername method is called to its proxy object (    That is, the current controller) sends a message, the parameter is to pass over the user name ~ Loginviewctrl.delegate = self; Key, code, all controllers have this method, show [self Presentviewcontroller:loginviewctrl animated:yes completion:^{NSLog (@ "Beyondlogin control    The system-appears "); }];} Implement the Proxy method in the next controller, because in the next controller (Loginviewctrl), after the user has entered the user name and password, the agent's Dosomethingwithusername method is called, the proxy object (that is, the current controller) is sent a message, the parameters is to pass over the user name ~-(void) Dosomethingwithloginname: (NSString *) username{username = [NSString stringwithformat:@ "Welcome back:%@",    Username];        _welcomelabel.text = Username;    Disable login button _wantloginbtn.enabled = NO; _wantlogoutbtn.enabled = YES;} -(Ibaction) Wantlogout: (UIButton *) sender{uiactionsheet *actionsheet = [[Uiactionsheet alloc]initwithtitle:@] ok to log off? "    Delegate:self cancelbuttontitle:@ "Cancel" destructivebuttontitle:@ "OK" otherbuttontitles:nil, nil]; [Actionsheet ShowInView:self.view];} #pragma mark-actionsheetProxy Method-(void) Actionsheet: (Uiactionsheet *) Actionsheet Clickedbuttonatindex: (nsinteger) buttonindex{NSLog (@ "button index:%d    ", Buttonindex);        Determine if the logoff index is 0 if (Buttonindex = = 0) {//Logoff _wantloginbtn.enabled = yes;        _wantlogoutbtn.enabled = NO;    _welcomelabel.text = @ ""; }//Cancel index is 1 doNothing} @end

Protocol

BeyondLoginViewControllerDelegate.h

  beyondloginviewcontrollerdelegate.h//  16_ Controller switching mode 1_modal////  Created by beyond on 14-7-30.//  Copyright (c) 2014 Com.beyond. All rights reserved.//#import <Foundation/Foundation.h> @protocol beyondloginviewcontrollerdelegate < nsobject>-(void) Dosomethingwithloginname: (NSString *) Username; @end


Beyondloginviewcontroller.xib


BeyondLoginViewController.h

  beyondloginviewcontroller.h//  16_ Controller switching mode 1_modal////  Created by Beyond on 14-7-30.//  Copyright (c) Com.beyond 2014. All rights reserved.//#import <UIKit/UIKit.h> #import "BeyondLoginViewControllerDelegate.h" @interface beyondloginviewcontroller:uiviewcontroller//the proxy of the ID type (weak weak reference), invokes the proxy method, passes the name entered by the user in this controller, passing the parameter to the proxy, for its use @property ( Nonatomic,weak) ID <BeyondLoginViewControllerDelegate> delegate; @property (weak, nonatomic) Iboutlet Uitextfield *username; @property (weak, nonatomic) Iboutlet Uitextfield *password;//submit a user name and password to the server-(ibaction) Submit: ( UIButton *) sender;//Click the Back button to return to the previous controller-(ibaction) Backtohome: (Uibarbuttonitem *) sender; @end


BeyondLoginVi EWCONTROLLER.M

beyondloginviewcontroller.m//16_ Controller Switching mode 1_modal////Created by Beyond on 14-7-30.//Copyright (c) 2014 Com.beyon D. All rights reserved.//#import "BeyondLoginViewController.h" #import "BeyondViewController.h" #import " BeyondLoginViewControllerDelegate.h "@interface Beyondloginviewcontroller () @end @implementation beyondloginviewcontroller-(ID) initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) nibbundleornil{self = [    Super Initwithnibname:nibnameornil Bundle:nibbundleornil]; if (self) {//Custom initialization} return to self;}    -(void) viewdidload{[Super Viewdidload]; Set Password box to asterisk _password.securetextentry = YES;} Click the Back button to return to the previous controller-(ibaction) Backtohome: (Uibarbuttonitem *) sender{//Key code, close itself this modal modal dialog box [self dismissviewcontr    Olleranimated:yes completion:^{NSLog (@ "Beyondlogin controller, disappeared"); }];} Submit user name and password to server-(ibaction) Submit: (UIButton *) Sender {//As JS form verification if (_username.text.length = = 0 | | _password.tex  T.length = = 0) {      <# (id<uiactionsheetdelegate>) #> uiactionsheet *actionsheet = [[Uiactionsheet alloc]initWithTit le:@ "Please enter user name and password" Delegate:nil cancelbuttontitle:@ "Cancel" destructivebuttontitle:@ "red Warning" otherbuttontitles:@ "other Buttons", nil]        ;                Like toast, manual display [Actionsheet ShowInView:self.view];    return directly;  }//Pass the data to the previous controller/////mode 1, the coupling is too strong, directly get the one that pops up (previous) controller Beyondviewcontroller *PREVC = (beyondviewcontroller            *) Self.presentingviewcontroller;    This allows you to set the label of the previous controller preVC.welcomeLabel.text = _username.text;    [Self dismissviewcontrolleranimated:yes completion:nil]; *////////2, using the proxy, the method of invoking the proxy, and the current controller input user name, as a parameter, passed to the agent in this method for the agent to use [Self.delegate Dosomethingwithloginname:_usernam    E.text]; [Self Dismissviewcontrolleranimated:yes completion:nil];} @end






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.