IOS foundation - the page jump several ways and by value

A, storyboard file jumps and traditional values ​​of viewController

1. Jump

First storyboard itself offers many convenient way to help us build the layout file, you can directly jump can be achieved between the viewContoller, storyboard can be seen as a collection of pages or viewController collection by "Connection" operation. This will not only reduce the amount of code we write, but also help us to see the logical jump between pages, here I say briefly, mode of operation:
the new few viewController, use and hold down the control button (press down the touch pad) , drag, there will be a line with an arrow, then move to viewController you want to jump in, release it, is one of several action options appear, the general election to show
Here Insert Picture DescriptionHere Insert Picture Description

2.Action Segue difference

Segue into several types, wherein Push, Modal, Popover and Replace deprecated.

  • Show
  • Show Detail
  • Present Modally
  • Present as Popover
  • Custom
  • Push
  • Modal
  • Popover
  • Replace

Show

  • The method provides an adaptive controller for the view, a flexible presentation.
  • When used UINavigationController stack view. Destination view navigation will be pressed into the top of the stack. Navigation bar provides a back button.
  • When used in UIViewController example, and present () consistent results.

Show Detail

  • Show and similar, but replacing the source view, and there is no back navigation buttons.

Present Modally

  • Use present ()
  • There are many different presentation, it can be set as desired. IPhone, generally in the form of animation from the bottom to cover up the entire screen, the user can not interact with on a view, unless you turn off the current view; in the iPad, commonly rendered as a center box, the center of the box to animate from the next pop up, while the bottom view controller darken.
  • Does not provide a return button.

Present as Popover

  • In the iPad, the target view to a floating window style rendering, click on the area outside the target view, the target view disappeared; the iPhone, the default view of the goal to cover the entire screen mode.

GIF below demonstrates the different ways of Show and Present appear in a new page:
Here Insert Picture Description

3. Reverse Transition

Present Modally return button is not provided, it is necessary to set the "reverse transition (unwind segue)"

The need to exit the controller is scheduled for a method: This method is only one parameter type must be UIStoryboardSegue, and there is a modifier @IBAction

@IBAction func close(segue:UIStoryboardSegue){
	let vc = segue.source as! ReviewViewController
	//反向传值
	print(vc.data)
}

Association specifies the reverse transition on your storyboard
the Ctrl Close button to drag view Exit, selection close:
Here Insert Picture Description

4. The value transfer

On the first page override prepare method, to be filled in vc.message the transfer of data.

    //使用SB连接转场每次都会触发下面方法
    override func prepare(segue: UIStoryboardSegue, sender: AnyObject?) {
        //可再每个连线处类似按钮的东西加上identifier,即可判断不同指向
        if segue.identifier == "segueIdentifier" {
 
            //不带导航的方式
             let vc = segue.destinationViewController as! nextVC
            //下个视图前了带了导航的方式
            //let nv_vc = segue.destinationViewController.childViewControllers[0] as! nextVC
            vc.message = "传值"
        }
    }

Second, the jump between viewController without any layout files

1. In the example UIViewController

present () method Dismiss () method, respectively.

    //present方式
    let vc = SecondViewController()
    self.present(vc, animated: true, completion: nil)
	//dismiss方式
	self.dismiss(animated:true,completion:nil)	

2. In the example UINavigationController

pushViewController () method popViewController () method, respectively.

	let viewController = ViewController()
	self.navigationController?.pushViewController(viewController, animated:true)

Return to previous view

	self.navigationController?.popViewController(animated:true)
	//使用匿名变量可以消除“返回值未使用”警告
	_ = self.navigationController?.popViewController(animated:true)

Return to the specified view

    //弹回根视图
    self.navigationController?.popToRootViewControllerAnimated(true)
    //指定位置
    self.navigationController?.popToViewController((self.navigationController?.viewControllers[0])!, animated: true)

Third, the jump between the Storyboard interface with viewController

    let sb = UIStoryboard(name: "Main", bundle:nil)
    let vc = sb.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
    
    self.presentViewController(vc, animated: true, completion: nil)

Note: Remember to set StoryboardID, you can modify the Identifier inspector in
Here Insert Picture Description

Fourth, by value

Forward pass value

Mode 1

In the first interface to add an action

@IBAction func touch(_ sender:AnyObject){
	let vc = ViewControllerTwo()
	vc.data = "第一个界面传入的数据"
	self.present(vc, animated:true, completion:nil)
}

Used in the second interface

override func viewDidLoad(){
	super.viewDidLoad()
	print(data)
}

Mode 2

To the second interface defines a new constructor

init(data:String){
	self.data = data
	super.init(nibName:nil, bundle:nil)
}

The new construction method using a first interface to instantiate classes ViewControllerTwo

@IBAction func touch(_ sender:AnyObject){
	let vc = ViewControllerTwo(data:"第一个界面传入的数据")
	self.present(vc, animated:true, completion:nil)
}

Reverse transfer value

Value greater than the forward pass reverse pass to complex values, the protocol generally used in the actual development or reverse pass value to complete the closure.

Mode 1 Use Agreement

Create a second interface protocol

protocol ViewControllerTwoProtocol {
	func setData(data:String)
}

var delegate:ViewControllerTwoProtocol?

When you click the Back button, be worth passing

	func ret(){
		delegate?.sentData(data:"第二个界面返回的值")
		self.dismiss(animated:true, completion:nil)
	}

The first method by value ViewControllerTwoProtocol interface to comply with the agreement, and to achieve agreement

class ViewController:UIViewController:UIViewController,ViewControllerTwoProtocol{
	@IBAction func touch(_ sender:AnyObject){
		let vc = ViewControllerTwo(data:"第一个界面传入的数据")
		//设置代理
		vc.delegate = self
		self.present(vc, animated:true, completion:nil)
	}

	func sentData(data:String){
		print(data)
	}
}

Embodiment 2 Using closures

Creating a closure at the second interface, and pass the value

	var closure:((String)->Void)?

	func ret(){
		self.closure!("第二个界面返回的值")
		self.dismiss(animated:true, completion:nil)
	}

A first interface for assignment closure

	@IBAction func touch(_ sender:AnyObject){
		let vc = ViewControllerTwo(data:"第一个界面传入的数据")
		//设置代理
		vc.closure = {(data:String) in
			print(data)
		}
		self.present(vc, animated:true, completion:nil)
	}
Published 43 original articles · won praise 1 · views 1078

Guess you like

Origin blog.csdn.net/weixin_42046829/article/details/104752685