前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ios实战-runloop实现的同步弹窗

ios实战-runloop实现的同步弹窗

作者头像
freesan44
发布2018-09-05 16:33:28
5800
发布2018-09-05 16:33:28
举报
文章被收录于专栏:freesan44freesan44

我们知道UIAlertView使用delegate返回数据实现的,使用麻烦,之前介绍过用Block实现的例子《ios实战-使用Block的UIAlertView》

今天介绍使用runloop实现,用return返回点击的结果的方式,首先看一下自定义弹窗的实现代码:

KSPopupView *popup = [[KSPopupView alloc] init];

NSIntegerbuttonIndex = [popup doModal];

NSLog(@"选择了%ld", (long)buttonIndex);

@implementationKSPopupView{BOOL_bModel;NSInteger_selectBtnIndex;

}

- (NSInteger)doModal {

[selfperformSelector:@selector(showAlert)];

_bModel =YES;while(_bModel) {

[[NSRunLoopmainRunLoop] runMode:NSDefaultRunLoopModebeforeDate:[NSDatedistantFuture]];

}return_selectBtnIndex;

}

- (void)showAlert {UIView*view = [[UIViewalloc] initWithFrame:CGRectMake(20,100,200,100)];

[view setBackgroundColor:[UIColorredColor]];UIButton*button = [[UIButtonalloc] initWithFrame:CGRectMake(0,0,50,30)];

[button setTitle:@"ok"forState:UIControlStateNormal];

[button setBackgroundColor:[UIColorgreenColor]];

[button addTarget:selfaction:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

[view addSubview:button];

[[UIApplicationsharedApplication].keyWindow addSubview:view];

}

- (void)buttonClick:(UIButton*)button {

[button.superview removeFromSuperview];

_selectBtnIndex =1;

_bModel =NO;

}

ok,没有问题,假如你想使用系统自带的UIAlertView的话,也是一样的,只是不要在程序刚启动的时候调用,不然会无法弹出(原因暂时还不知道),下面是UIAlertView的例子:

- (NSInteger)doModal {

[selfshowAlert];

_bModel =YES;while(_bModel) {

[[NSRunLoopmainRunLoop] runMode:NSDefaultRunLoopModebeforeDate:[NSDatedistantFuture]];

}return_selectBtnIndex;

}

- (void)showAlert {UIAlertView*alertView = [[UIAlertViewalloc] initWithTitle:@""message:@"okookoko"delegate:selfcancelButtonTitle:@"cancel"otherButtonTitles:@"ok",nil];

[alertView show];

}

- (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

_selectBtnIndex = buttonIndex;

_bModel =NO;

}

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.02.17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档