您好,欢迎来到二三娱乐。
搜索
您的当前位置:首页runloop实现的同步弹窗

runloop实现的同步弹窗

来源:二三娱乐

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

KSPopupView *popup = [[KSPopupView alloc] init];
NSInteger buttonIndex = [popup doModal];
NSLog(@"选择了%ld", (long)buttonIndex);
@implementation KSPopupView {
BOOL _bModel;
NSInteger _selectBtnIndex;
}

  • (NSInteger)doModal {
    [self performSelector:@selector(showAlert)];

    _bModel = YES;
    while (_bModel) {
    [[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }

    return _selectBtnIndex;
    }

  • (void)showAlert {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20, 100, 200, 100)];
    [view setBackgroundColor:[UIColor redColor]];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    [button setTitle:@"ok" forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor greenColor]];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    [[UIApplication sharedApplication].keyWindow addSubview:view];
    }

  • (void)buttonClick:(UIButton *)button {
    [button.superview removeFromSuperview];
    _selectBtnIndex = 1;
    _bModel = NO;
    }
    ok,没有问题,假如你想使用系统自带的UIAlertView的话,也是一样的,只是不要在程序刚启动的时候调用,不然会无法弹出(原因暂时还不知道),下面是UIAlertView的例子:

  • (NSInteger)doModal {
    [self showAlert];
    _bModel = YES;
    while (_bModel) {
    [[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }

    return _selectBtnIndex;
    }

  • (void)showAlert {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:@"okookoko" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
    [alertView show];
    }

  • (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    _selectBtnIndex = buttonIndex;
    _bModel = NO;
    }

Copyright © 2019- yule263.com 版权所有 湘ICP备2023023988号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务