Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用动态视图处理导航?_Ios_Swift_Uiview_Uiscrollview - Fatal编程技术网

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

Ios 如何使用动态视图处理导航?,ios,swift,uiview,uiscrollview,Ios,Swift,Uiview,Uiscrollview,我正在向UIScrollView添加自定义UIView,每个UIView都有一个按钮,需要打开一些用户配置文件,这些用户配置文件必须在新的UIViewController中打开。但是我如何用这个按钮创建和连接一个新的控制器呢 这是我的密码: class FollowersViewController: UIViewController { @IBOutlet weak var scrollView: UIScrollView! override func viewDidLoad() {

我正在向UIScrollView添加自定义UIView,每个UIView都有一个按钮,需要打开一些用户配置文件,这些用户配置文件必须在新的UIViewController中打开。但是我如何用这个按钮创建和连接一个新的控制器呢

这是我的密码:

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
    }



}
类FollowersViewController:UIViewController{
@iBCROLLVIEW:UIScrollView!
重写func viewDidLoad(){
super.viewDidLoad()
对于变量i=0;i

如果我的CustomView中有一个按钮打开不同的(可能是动态的)视图控制器(取决于其内容)如何处理此问题?

向您的
视图添加
按钮
,并为其分配标记。根据此标记,您可以使用开关盒检查哪个按钮调用选择器
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
    }
}
override func viewDidLoad(){
super.viewDidLoad()
对于变量i=0;i
您可以尝试修改CustomView以接受1个参数作为UIViewController。然后在添加手势时将此控制器设置为目标

代码:

您的CustomView初始化:

func init(controller: UIViewController, frame: CGRect)
和您的视图手势识别器:

followGesture = UITapGestureRecognizer(target: controller, action: "follow:") 
follow.addGestureRecognizer(followGesture)
然后,在FollowersViewController中,您可以实现目标功能:

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

你能展示你的FollowerView实现吗?我能,但没什么意义,它工作正常,但我不知道如何处理let
followspirate=UITapGestureRecognitizer(目标:self,动作:“follow:”)follow.addGestureRecognitizer(followspirate)
上的事件以打开新窗口(ViewController)这是一个不错的尝试,但是如果动态创建视图,我不知道它们的数量,也不能为所有视图创建开关状态。但是如果我可以使用标记作为某些函数的参数,比如:
func actionForButton(sender:UIButton){opneVievController(sender.tag)}
我可以这样做吗?当然。假设
opneVievController
是您的函数,它使用参数打开相应的视图控制器。这对我来说很有效,但为此目的,委托可能是正确的用法。与您相比,我认为这可以根据我的需要工作,所以我这样做……这样做的目的好吗,还是会产生问题?我无法回答您r问题,因为我是iOS的新手)。也许有人可以解释这样做的危险性,但我不能,对不起)。我在需要时使用它,它可以工作。。
func follow(sender: UITapGestureRecognizer) {
    // do your stuff
}