如何使用动态视图处理导航 iOS Swift?
How to handle navigation with dynamic views iOS Swift?
我正在向我的 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
}
}
如果我的 CustomView 中有一个按钮可以根据其内容打开不同的(可能是动态的)ViewController,我该如何处理这个问题?
将 button
添加到您的 view
,并为其分配标签。基于此标记,您可以使用 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
}
}
您可以尝试修改您的 CustomView 以接受 1 个参数作为 UIViewController。然后当您添加手势时,将此控制器设置为目标。
代码:
view = FollowerView(self, frame: CGRectMake(0, scrollHeight, self.view.frame.width, 62))
您的自定义视图初始化:
func init(controller: UIViewController, frame: CGRect)
以及您的视图 gestureRecognizer:
followGesture = UITapGestureRecognizer(target: controller, action: "follow:")
follow.addGestureRecognizer(followGesture)
然后在您的 FollowersViewController 中您可以实现您的目标功能:
func follow(sender: UITapGestureRecognizer) {
// do your stuff
}
我正在向我的 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
}
}
如果我的 CustomView 中有一个按钮可以根据其内容打开不同的(可能是动态的)ViewController,我该如何处理这个问题?
将 button
添加到您的 view
,并为其分配标签。基于此标记,您可以使用 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
}
}
您可以尝试修改您的 CustomView 以接受 1 个参数作为 UIViewController。然后当您添加手势时,将此控制器设置为目标。
代码:
view = FollowerView(self, frame: CGRectMake(0, scrollHeight, self.view.frame.width, 62))
您的自定义视图初始化:
func init(controller: UIViewController, frame: CGRect)
以及您的视图 gestureRecognizer:
followGesture = UITapGestureRecognizer(target: controller, action: "follow:")
follow.addGestureRecognizer(followGesture)
然后在您的 FollowersViewController 中您可以实现您的目标功能:
func follow(sender: UITapGestureRecognizer) {
// do your stuff
}