24

如何将变量传递给UIAlertView委托?

我有一个要在警报视图委托中使用的变量。它仅用于显示UIAlertViewUIAlertView委托的函数中,所以我认为它不应该是控制器上的属性。有没有办法将变量附加到UIAlertView委托并在委托中检索它?

- (void) someUserCondition:(SOCode *)userCode {
    if ([userCode warrentsConfirmation] > 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Are you sure?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil];        
        [alert setAlertViewStyle:UIAlertViewStyleDefault];  
        //TODO somehow store the code variable on the alert view
        [alert show];
    }
}

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex   {
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if ([title isEqualToString:@"OK"]){
       SOCode *userCode = //TODO somehow get the code from the alert view
       [self continueWithCode:code];
    }                                 
}
4

6 回答 6

26

在接口之前的 .h 中:

extern const char MyConstantKey;
@interface ViewController...

在 .m 导入中:

import <objc/runtime.h>

在 .m 实施前

const char MyConstantKey;

在 .m 实现中

-(void)viewDidAppear:(BOOL)animated{ //or wherever

    NSString *aString = @"This is a string";

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Testing" message:@"test is test" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil];

    [alert show];

    [alert release];

    objc_setAssociatedObject(alert, &MyConstantKey, aString, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

 }

在 .m alertview 回调中

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

     NSString *associatedString = objc_getAssociatedObject(alertView, &MyConstantKey);

     NSLog(@"associated string: %@", associatedString);

}
于 2012-04-30T21:00:37.537 回答
14

使用关联对象。这里有更详细的描述:你的新朋友:Obj-C 关联对象

要设置您使用的对象,请使用:

objc_setAssociatedObject(alert, &key, userCode, OBJC_ASSOCIATION_RETAIN);

然后将其取回:

SOCode *userCode = objc_getAssociatedObject(alertView, &key);

您还需要添加static char key;使其在 moth 方法的范围内。

更新

我已将其包装到UIAlertView. 您可以使用 Cocoapods 将其引入:

pod 'HCViews/UIAlertViewHCContext', '~> 1.2'

源代码在这里:https ://github.com/hypercrypt/HCViews/blob/master/Categories/UIAlertView%2BHCContext.h

于 2012-04-30T20:36:58.283 回答
7

A lot of posts talk about the concepts behind associated objects (which is good!) but sometimes you just want to see the code. Here's a clean and quick category that you can either put in a separate file or above the interface of one of your existing .m files (you could even replace UIAlertView with NSObject and effectively add a context property to any object):

#import <objc/runtime.h>

@interface UIAlertView (Private)
@property (nonatomic, strong) id context;
@end

@implementation UIAlertView (Private)
@dynamic context;
-(void)setContext:(id)context {
    objc_setAssociatedObject(self, @selector(context), context, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(id)context {
    return objc_getAssociatedObject(self, @selector(context));
}
@end

And then you'll be able to do something like:

NSObject *myObject = [NSObject new];

UIAlertView *alertView = ...
alertView.context = myObject;

IMPORTANT: And don't forget to nil the context in dealloc!!

于 2013-09-05T21:20:46.520 回答
3

UIAlertView是一个子类,UIView它有一个tag可以设置为整数的属性。不幸的是,如果您需要整数以外的东西来识别/将信息传递给委托,那么您将需要在委托本身上设置一些属性(或设置一个带有标签索引的数组)。Advaith 的方式可能会奏效,但在技术上不受 Apple 支持。

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Are you sure?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil];        
    [alert setAlertViewStyle:UIAlertViewStyleDefault];  
    alert.tag = SOMEINTEGER;
    [alert show];
于 2012-04-30T20:44:45.377 回答
1

我怀疑最直接的方法是警报视图的委托类中的属性。警报视图没有“用户信息”的任何规定,也不支持子分类,这删除了唯一能想到的快捷方式。

于 2012-04-30T17:35:17.760 回答
0

子类 UIAlertView,添加一个名为 userInfo 的属性,该属性具有您选择的类型。在创建 Subclassed UIAlertView 的实例时设置用户信息值,并从委托方法中检索它。(在那里您将获得包含 userInfo 的子类实例)

于 2012-04-30T20:32:46.260 回答