这个只是一个按钮的点击逻辑,界面中存在 N 个按钮,与这个逻辑类似。于是 vc 中充斥大量的 逻辑判断,网络调用,弹窗调用,界面跳转,界面刷新等。请问怎么优化呢? (继之前的主题,可能之前没描述好 https://www.v2ex.com/t/368557#reply11 )
//版本 v2
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//macro.h
#ifndef weakify
#if __has_feature(objc_arc)
#define weakify( x ) autoreleasepool{} __weak __typeof__(x) __weak_##x##__ = x;
#else // #if __has_feature(objc_arc)
#define weakify( x ) autoreleasepool{} __block __typeof__(x) __block_##x##__ = x;
#endif // #if __has_feature(objc_arc)
#endif // #ifndef weakify
#ifndef normalize
#if __has_feature(objc_arc)
#define normalize( x ) try{} @finally{} __typeof__(x) x = __weak_##x##__;
#else // #if __has_feature(objc_arc)
#define normalize( x ) try{} @finally{} __typeof__(x) x = __block_##x##__;
#endif // #if __has_feature(objc_arc)
#endif // #ifndef @normalize
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//store/pref/ASHPrefStore.h
@interface ASHPrefStore : NSObject
+ (BOOL)haveReadBroadcastTip;
+ (void)setHaveReadBroadcastTip;
@end
//store/pref/ASHPrefStore.m
@implementation ASHPrefStore
static NSString *const kHaveReadBroadcastTipKey = @"haveReadBroadcastTip";
+ (BOOL)haveReadBroadcastTip
{
return [[NSUserDefaults standardUserDefaults] boolForKey:kHaveReadBroadcastTipKey];
}
+ (void)setHaveReadBroadcastTip
{
[[NSUserDefaults standardUserDefaults] setObject:@(YES) forKey:kHaveReadBroadcastTipKey];
}
@end
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//network/ASHNetworkAPI.h
@interface ASHNetworkAPI : NSObject
+ (void)sendBroadcastRequestWithMsg:(NSString *)msg successBlock:(void(^)(void))successBlock failureBlock:(void(^)(NSError *error))failureBlock;
@end
//network/ASHNetworkAPI.m
@implementation ASHNetworkAPI
+ (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();
});
}
@end
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//utils/ui/ASHAlertUtils.h
@interface ASHAlertUtils : NSObject
+ (void)showAlertWithController:(UIViewController *)controller msg:(NSString *)msg callback:(void(^)(void))callback;
+ (void)showEditAlertWithController:(UIViewController *)controller title:(NSString *)title placeholder:(NSString *)placeholder callback:(void(^)(NSString *content))callback;
@end
//utils/ui/ASHAlertUtils.m
@implementation ASHAlertUtils
#pragma mark - utils
+ (void)showAlertWithController:(UIViewController *)controller msg:(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();
}]];
[controller presentViewController:alert animated:YES completion:nil];
}
+ (void)showEditAlertWithController:(UIViewController *)controller title:(NSString *)title placeholder:(NSString *)placeholder callback:(void(^)(NSString *content))callback
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = placeholder;
}];
[alertController addAction:[UIAlertAction actionWithTitle:@"发送" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *content = [[alertController textFields][0] text];
!callback ?: callback(content);
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"Canelled");
}]];
[controller presentViewController:alertController animated:YES completion:nil];
}
@end
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//controller/ASHTempViewController.m
@implementation ASHTempViewController
#pragma mark - app logic
- (void)sendBroadcastWithSuccessBlock:(void (^)(void))successBlock
{
@weakify(self)
void (^showSendBroadcast) (void) = ^{
[ASHAlertUtils showEditAlertWithController:self title:@"发送广播'" placeholder:@"说点什么..." callback:^(NSString *content) {
//可能增加的业务 : 这里再做一些长度限制检查
[ASHNetworkAPI sendBroadcastRequestWithMsg:content successBlock:^{
@normalize(self)
//可能增加的业务 : 做一些数据更新 和 界面刷新
!successBlock ?: successBlock();
} failureBlock:^(NSError *error) {
@normalize(self)
!self ?: [ASHAlertUtils showAlertWithController:self msg:error.localizedDescription callback:nil];
}];
}];
};
if ([ASHPrefStore haveReadBroadcastTip]) {
showSendBroadcast();
} else {
NSString *title = @"1、发布广播,全服玩家都可以看到;\n2、禁止发布反动、政治、色情、辱骂、广告等不良言论,否则将会遭到删除、封号处理。";
[ASHAlertUtils showAlertWithController:self msg:title callback:^{
[ASHPrefStore setHaveReadBroadcastTip];
showSendBroadcast();
}];
}
}
@end
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.