简体   繁体   中英

UIViewController present and dismiss

There are 3 viewControllers available in my code.i wrote the code to present 3rd view controller from first view controller.

There are 2 buttons available in 3rd view controller.(done and cancel).when i tap the done button 2nd view controller need to be present.

How to write the code for that?

Firstly I recommend you must search and look at Objective-C documentation and examples. But there is basic of how to present a UIViewController .

SecondViewController *controller = [SecondViewController new];

if you want to use completion block

[self presentViewController:controller animated:YES completion:^{

}];

or if you want to use as just present.

[self presentViewController:controller animated:YES completion:nil];

// Edit Section

So I assume the the buttons like below.

UIButton *toGoSecond;
UIButton *toGoThird;

Then in viewDidLoad method you can assign the actions for these buttons.

[toGoSecond addTarget:self action:@selector(goToSecond) forControlEvents:UIControlEventTouchUpInside];
    [toGoThird addTarget:self action:@selector(goToThird) forControlEvents:UIControlEventTouchUpInside];

And there is the presentation handler functions.

-(void) goToSecond{
    SecondController *second = [SecondController new];
    [self presentViewController:second animated:TRUE completion:nil];
}

-(void) goToThird{
    ThirdController *thirdController = [ThirdController new];
    [self presentViewController:thirdController animated:TRUE completion:nil];
}

I think a little bit searching and looking at tutorials can make you clear about your problem, I hope the edited answer helps for you.

// LAST EDIT

Hey, I can't get your problem completely when I answer this but I handle your problem with delegation pattern.

I've created 3 controllers named as ViewController , SecondViewController , ThirdViewController .

So here we go.

Create a protocol.

@protocol ProtocolName
-(void) go;
@end

Then assign it to your first view controller as below.

@interface ViewController : UIViewController<ProtocolName>

Than in ViewController.m file fill the go method.

- (void)go{
    NSLog(@"triggered");
    SecondViewController *second = [SecondViewController new];
    [self presentViewController:second animated:TRUE completion:nil];
}

Then in ThirdController.h file put the delegate as weak variable.

@interface ThirdViewController : UIViewController
@property (nonatomic,weak) id<ProtocolName> delegate;
@end

Before the go to ThirdViewController from the FirstViewController assign the delegate of it like below.

-(void) goToThird{
    ThirdViewController *thirdController = [ThirdViewController new];
    [thirdController setDelegate:self];
    [self presentViewController:thirdController animated:TRUE completion:nil];
}

Then if you press the your button for present SecondViewController , implement a button action method like below.

- (void) targetMethod{
    [self dismissViewControllerAnimated:YES completion:nil];
    [_delegate go];
}

Firstly you have to dismiss your current ThirdViewController then delegate has working and presenting SecondViewController .

you can use delegate.. Using Delegate you can call 1st View controller method to present 2nd View Controller from your 3rd view controller Done Button

Example code: FirstViewController.swift

import UIKit

public protocol GoToSecondVCDelegate : class {
func gotoSecondVC()
}

class FirstViewController: UIViewController, GoToSecondVCDelegate   {

override func viewDidLoad() {
    super.viewDidLoad()

}

@IBAction func gotoThirdVC(sender : UIButton){
    let thirdVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ThirdViewController") as! ThirdViewController
    thirdVC.delegate = self
    self.present(thirdVC, animated: true, completion: nil)
}
func gotoSecondVC() {
    let secondVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
    self.present(secondVC, animated: true, completion: nil)
}
}

SecondViewController.swift

import UIKit

class SecondViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}
@IBAction func cancelClick(sender : UIButton)
{
    self.dismiss(animated: true, completion: nil)

}
}

ThirdViewController.swift

import UIKit

class ThirdViewController: UIViewController {

var delegate : GoToSecondVCDelegate!
override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}
@IBAction func doneClick(sender : UIButton)
{
    self.dismiss(animated: true, completion: nil)
    delegate.gotoSecondVC()
}
@IBAction func cancelClick(sender : UIButton)
{
     self.dismiss(animated: true, completion: nil)
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM