简体   繁体   English

如何使用动态视图iOS Swift处理导航?

[英]How to handle navigation with dynamic views iOS Swift?

I am adding custom UIViews to my UIScrollView, and every UIView has a button that needs to open some users profile which must open in new UIViewController. 我将自定义UIView添加到我的UIScrollView中,并且每个UIView都有一个按钮,需要打开一些用户配置文件,该配置文件必须在新的UIViewController中打开。 But how do i create and connect a new controller with that button? 但是,如何创建新的控制器并将其与该按钮连接?

Here is my code: 这是我的代码:

class FollowersViewController: UIViewController {

@IBOutlet weak var scrollView: UIScrollView!


override func viewDidLoad() {
    super.viewDidLoad()
    for  var i = 0; i < count; i++ {

        view = FollowerView(frame: CGRectMake(0, scrollHeight, self.view.frame.width, 62))

        scrollView.addSubview(view)
        scrollHeight += view.frame.height
        scrollView.contentSize.height += view.frame.height
    }



}

If there is a button inside my CustomView that opens different (maybe dynamic) ViewControllers depending on its content how can I handle this issue? 如果我的CustomView中有一个按钮,根据其内容打开不同的(也许是动态的)ViewController,我该如何处理此问题?

Add a button to your view , and assign a tag to it. view添加一个button ,并为其分配标签。 Based on this tag, you can check which button is calling the selector actionForButton using a switch-case: 基于此标记,您可以使用切换用例来检查哪个按钮正在调用选择器actionForButton

override func viewDidLoad() {
    super.viewDidLoad()
    for  var i = 0; i < count; i++ {
        var view: CustomView = followers[i]
        view = FollowerView(frame: CGRectMake(0, scrollHeight, self.view.frame.width, 62))

        var button = UIButton(frame: CGRectMake(0, 0, 40, 40))        
        button.addTarget(self, action: "actionForButton", forControlEvents: .TouchUpInside)
        button.tag = i;
        view.addSubview(button)

        scrollView.addSubview(view)
        scrollHeight += view.frame.height
        scrollView.contentSize.height += view.frame.height
    }
}


func actionForButton(sender: UIButton) {

    switch (sender.tag)
    {

    case 0: //perform related task
        break

    case 1: //perform related task
        break
    }
}

You can try to modify your CustomView to accept 1 parameter as UIViewController. 您可以尝试将CustomView修改为接受1个参数作为UIViewController。 And then when your are adding gesture set this controller as target. 然后,当您添加手势时,将此控制器设置为目标。

Code: 码:

view = FollowerView(self, frame: CGRectMake(0, scrollHeight, self.view.frame.width, 62))

Your CustomView init: 您的CustomView初始化:

func init(controller: UIViewController, frame: CGRect)

And your view gestureRecognizer: 和您的视图手势识别器:

followGesture = UITapGestureRecognizer(target: controller, action: "follow:") 
follow.addGestureRecognizer(followGesture)

Then in your FollowersViewController you can implement your target function: 然后可以在FollowersViewController中实现目标函数:

func follow(sender: UITapGestureRecognizer) {
    // do your stuff
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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