我正在尝试在我的应用程序中实现一个 UIActivityView
。它已经可以工作了,但我现在需要做的是在 AppDelegate
中创建一个方法供所有其他类使用,因此我不需要重复代码。
在我的 AppDelegate
中,我有以下方法:
- (void)acionarActivityIndicator {
NSLog(@"mETODO APPDELEGATE");
// Show the activity indicator
UIView *view = [[UIView alloc] init];
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] init];
view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
view.frame = self.window.bounds;
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
CGRect frame = view.frame;
activityIndicator.center = CGPointMake(frame.size.width/2, frame.size.height/2);
[view addSubview:activityIndicator];
[activityIndicator startAnimating];
[self.window addSubview:view];
[self.window makeKeyAndVisible];
}
该方法在 @protocol
.h 文件中声明。
@protocol ActivityIndicatorProtocol <NSObject>
@required
- (void)acionarActivityIndicator;
@end
现在,在 buttonClick
或其他任何地方,我需要将此 View 放在事件 VC 上。为此,我将 AppDelegate
设置为我的 VC 的 delegate
。
我在 VC 的 viewDidLoad
上有这个:
ITAppDelegate *appDelegate = [[ITAppDelegate alloc] init];
self.delegate = appDelegate;
NSLog(@"DELEGATE %@", self.delegate);
这是正确分配委托(delegate)。委托(delegate) @property
在我的 VC .h 中声明如下:
@property(nonatomic, assign)id 委托(delegate);
现在,同样在 viewDidLoad
中,出于测试目的,我尝试通过调用 AppDelegate
中的方法来显示 UIActivityIndicatorView
。
[self.delegate acionarActivityIndicator];
这行不通,我不知道为什么。我可以使用其他策略放置 activityIndicators,但我真的很想学习如何正确地执行此操作。
最佳答案
尝试使用
ITAppDelegate *appDelegate = (ITAppDelegate *)[[UIApplication sharedApplication] delegate];
代替
ITAppDelegate *appDelegate = [[ITAppDelegate alloc] init];
关于ios - AppDelegate 中的 UIActivityView 使用协议(protocol)和委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21022081/