iOS: 请问大家下面这段 ViewController 的代码( app logic 的部分)怎么优化?我不想在 VC 里面放这些琐碎的代码怎么怎么搞?

2017-06-15 12:09:35 +08:00
 summer1991
@implementation ASHTempViewController

#pragma mark - app logic

- (void)sendBroadcastWithSuccessBlock:(void (^)(void))successBlock
{
    BOOL haveRead = [[NSUserDefaults standardUserDefaults] boolForKey:@"haveReadBroadcastTip"];
    if (haveRead == NO) {
        NSString *title = @"1、发布广播,全服玩家都可以看到;\n2、禁止发布反动、政治、色情、辱骂、广告等不良言论,否则将会遭到删除、封号处理。";
        __weak typeof(self) wself = self;
        [self showAlertWithMsg:title callback:^{
            [[NSUserDefaults standardUserDefaults] setObject:@(YES) forKey:@"haveReadBroadcastTip"];
            [wself showSendViewWithSuccessBlock:successBlock];
        }];
    } else {
        [self showSendViewWithSuccessBlock:successBlock];
    }
}

- (void)showSendViewWithSuccessBlock:(void(^)(void))successBlock
{
    __weak typeof(self) wself = self;
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"发送广播" message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"说点什么...";
    }];
    [alertController addAction:[UIAlertAction actionWithTitle:@"发送" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSString *msg = [[alertController textFields][0] text];
        [wself sendBroadcast:msg successBlock:successBlock];
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"Canelled");
    }]];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)sendBroadcast:(NSString *)msg successBlock:(void(^)(void))successBlock
{
    __weak typeof(self) wself = self;
    [self sendBroadcastRequestWithMsg:msg successBlock:^{
        //data operation
        !successBlock ?: successBlock();
    } failureBlock:^(NSError *error) {
        [wself showAlertWithMsg:error.localizedDescription callback:nil];
    }];
}

#pragma mark - networks

- (void)sendBroadcastRequestWithMsg:(NSString *)msg successBlock:(void(^)(void))successBlock failureBlock:(void(^)(NSError *error))failureBlock
{
    //send request
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        !successBlock ?: successBlock();
    });
}

#pragma mark - utils

- (void)showAlertWithMsg:(NSString *)msg callback:(void(^)(void))callback
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:msg preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        !callback ?: callback();
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}

@end
4132 次点击
所在节点    iDev
22 条回复
summer1991
2017-07-03 11:32:18 +08:00
@winglight2016 有好的想法吗亲?
winglight2016
2017-07-04 12:37:27 +08:00
@summer1991 要求能够弹窗,这个需要完整框架的支持了,不是普通代码能做的,你可以参考一下 MVVM 的框架

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/368557

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX