ios - AppDelegate 中的 UIActivityView 使用协议(protocol)和委托(delegate)

标签 ios objective-c uiviewcontroller delegates

我正在尝试在我的应用程序中实现一个 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 中的方法来显示 UIActivityIndi​​catorView

[self.delegate acionarActivityIndi​​cator];

这行不通,我不知道为什么。我可以使用其他策略放置 activityIndi​​cators,但我真的很想学习如何正确地执行此操作。

最佳答案

尝试使用

ITAppDelegate *appDelegate = (ITAppDelegate *)[[UIApplication sharedApplication] delegate];

代替

ITAppDelegate *appDelegate = [[ITAppDelegate alloc] init];

关于ios - AppDelegate 中的 UIActivityView 使用协议(protocol)和委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21022081/

相关文章:

ios - 如果当前嵌套很深,如何返回根 ViewController?

ios - Swift iOS - 将 View Controller 添加到新的 keyWindow 时 deinit 运行

iphone - 在 iPhone 上显示弹出式透明窗口的最佳方法?

ios - 如何在 iOS 项目中正确配置 Realm DB 文件 (<db_name>.realm) 的路径?

ios - 有没有办法在不快速使用 NSAttributedString 类的情况下创建属性字符串?

objective-c - 如何处理 NSString 中的 32 位 unicode 字符

ios - 如何在我输入 UITextField 时获取谷歌建议并将其放在 UITableView 上

iphone - 我在单例中使用保留还是复制?

ios - 带有数组字符串的子字符串 - Swift

iOS 8.4 UITabelview scrollToRowAtIndexPath 在设备中不起作用