[英]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.我的代码中有 3 个视图控制器可用。我编写了代码以从第一个视图 controller 呈现第三个视图 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.第三视图 controller 中有 2 个按钮可用。(完成和取消)。当我点击完成按钮时,需要存在第二视图 controller。
How to write the code for that?如何为此编写代码?
Firstly I recommend you must search and look at Objective-C
documentation and examples.首先,我建议您必须搜索并查看
Objective-C
文档和示例。 But there is basic of how to present a UIViewController
.但是有基本的如何展示
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.然后在
viewDidLoad
方法中,您可以为这些按钮assign
操作。
[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
.我创建了 3 个控制器,分别命名为
ViewController
、 SecondViewController
、 ThirdViewController
。
So here we go.所以这里我们 go。
Create a protocol.创建一个协议。
@protocol ProtocolName
-(void) go;
@end
Then assign it to your first view controller as below.然后将其分配给您的第一个视图 controller,如下所示。
@interface ViewController : UIViewController<ProtocolName>
Than in ViewController.m file fill the go method.比在 ViewController.m 文件中填写 go 方法。
- (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.然后在
ThirdController.h
文件中将delegate
作为weak
变量。
@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.在从 FirstViewController 到
ThirdViewController
的FirstViewController
分配它的委托,如下所示。
-(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.然后,如果您按下当前
SecondViewController
的按钮,请实现如下所示的按钮操作方法。
- (void) targetMethod{
[self dismissViewControllerAnimated:YES completion:nil];
[_delegate go];
}
Firstly you have to dismiss your current ThirdViewController
then delegate has working and presenting SecondViewController
.首先,您必须关闭当前的
ThirdViewController
然后委托有工作并呈现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您可以使用委托.. 使用委托您可以调用 1st View controller 方法从您的 3rd View controller 按钮显示 2nd View Controller
Example code: FirstViewController.swift示例代码: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 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第三视图控制器.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)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.