IOS-oc中label标签添加点击事件并跳转页面

一,概述

在网上找点击和跳转的方法找了很久,没有一个很规整的,这里记录一下,方便以后查找,也希望给需要的人一点帮助。

二,代码

首先是实现label的点击事件:

@property (strong, nonatomic) IBOutlet UILabel *topage;
这里直接
说点击事件的实现:

自定义一个点击后调用的方法:

-(void)touchAble:(UITapGestureRecognizer *)rec{
    

}

给标签注册添加点击事件:

//设置可点击
    topage.userInteractionEnabled=YES;
    //创建点击事件,点击的时候触发touchAble
    UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchAble:)];
    //添加点击事件
    [topage addGestureRecognizer:rec];


这里点击后就会调用上面的 touchAble方法。

然后是cotroller跳转实现:

我这里目标controller叫SecondController

    SecondController * sec = [[SecondController alloc]init];
    sec.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:sec animated:YES completion:nil];

这样就实现了点击跳转页面。

顺便说一句,返回前一个页面执行的方法:

    //将弹出的模拟视图移除,第一个参数yes表示移除的时候带有动画效果;第二个参数是一个回调方法,当模拟视图移除消失后会调用。
    //不写回调方法这样调用[self dismissViewControllerAnimated:YES completion:nil];或者按下面这样带回调方法
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"back");
        //ios 5.0以上可以用该方法
    }];



猜你喜欢

转载自blog.csdn.net/liujibin1836591303/article/details/53171383