添加UITapGestureRecognizer以动态添加视图

时间:2021-08-17 05:32:48

Im having some issues with adding tapRecognizers to my views. Im adding an unknown number of events, news and coupons to my UIScrollView where on click i want to open an detail view. However on click the app crashes with following error

我在将tapRecognizers添加到我的视图时遇到了一些问题。我将未知数量的事件,新闻和优惠券添加到我的UIScrollView,点击我想打开详细视图。但是,单击该应用程序崩溃时出现以下错误

Almhults_appen.MainActivity redirectFromHomeScreen:]: unrecognized selector sent to instance 0x7f93c2d4df20

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Almhults_appen.MainActivity redirectFromHomeScreen:]: 

As I see it I could make use of an UITableView and add all views to it. However if possible I would personally like to avoid it but since I'm new to Swift I don't trust my judgement here.

在我看来,我可以使用UITableView并添加所有视图。但是,如果可能的话,我个人希望避免它,但是因为我是Swift的新手,所以我不相信我的判断。

if (!event_ar.isEmpty) {
    for event: Event in event_ar {

        ... Init EventView and add to UIScrollView

        // Add tapGesture
        let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(
            target: self,
            action: "redirectFromHomeScreen:"
        )
        eventView.addGestureRecognizer(tapGesture)
    }
}
if (!news_ar.isEmpty) {
    ... Add news identically to events
}
if (!coupon_ar.isEmpty) {
    ... Add coupons identically to events
}

Edit added action function

编辑添加的动作功能

private func redirectFromHomeScreen(sender: UITapGestureRecognizer) -> Void{
    ... Do stuff
}

Thanks in advance :)

提前致谢 :)

3 个解决方案

#1


0  

According to your crash report .. first you need to declare funtion

根据您的崩溃报告..首先您需要声明功能

func redirectFromHomeScreen (sender : UITapGestureRecognizer){

}

Declare this and try again

声明并重试

#2


0  

You have to add your gesture to the view you will tap on. And the best moment to add it is within that view's viewDidLoad method. With Objective-C I do that and it works well.

您必须将手势添加到要点按的视图中。添加它的最佳时刻是在该视图的viewDidLoad方法中。使用Objective-C我这样做并且效果很好。

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIPanGestureRecognizer  *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(PanToFlipPage:)];
    [self.view addGestureRecognizer:pan];

}

}

#3


0  

Try to add these lines after you have created all of your subviews and buttons.

在创建所有子视图和按钮后尝试添加这些行。

[self.view setNeedsLayout];
[self.view layoutIfNeeded];

#1


0  

According to your crash report .. first you need to declare funtion

根据您的崩溃报告..首先您需要声明功能

func redirectFromHomeScreen (sender : UITapGestureRecognizer){

}

Declare this and try again

声明并重试

#2


0  

You have to add your gesture to the view you will tap on. And the best moment to add it is within that view's viewDidLoad method. With Objective-C I do that and it works well.

您必须将手势添加到要点按的视图中。添加它的最佳时刻是在该视图的viewDidLoad方法中。使用Objective-C我这样做并且效果很好。

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIPanGestureRecognizer  *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(PanToFlipPage:)];
    [self.view addGestureRecognizer:pan];

}

}

#3


0  

Try to add these lines after you have created all of your subviews and buttons.

在创建所有子视图和按钮后尝试添加这些行。

[self.view setNeedsLayout];
[self.view layoutIfNeeded];