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

wus*_*her 24 iphone objective-c uialertview ios

如何将变量传递给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];
    }                                 
}
Run Code Online (Sandbox Code Playgroud)

war*_*eed 26

在.h之前的界面:

extern const char MyConstantKey;
@interface ViewController...
Run Code Online (Sandbox Code Playgroud)

在.m导入:

import <objc/runtime.h>
Run Code Online (Sandbox Code Playgroud)

在.m之前实施

const char MyConstantKey;
Run Code Online (Sandbox Code Playgroud)

在.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);

 }
Run Code Online (Sandbox Code Playgroud)

在.m alertview回调

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

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

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

}
Run Code Online (Sandbox Code Playgroud)


hyp*_*ypt 14

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

要设置您使用的对象:

objc_setAssociatedObject(alert, &key, userCode, OBJC_ASSOCIATION_RETAIN);
Run Code Online (Sandbox Code Playgroud)

然后把它拿回来:

SOCode *userCode = objc_getAssociatedObject(alertView, &key);
Run Code Online (Sandbox Code Playgroud)

您还需要添加static char key;以使其处于moth方法的范围内.

更新

我把它包装成了一个类别UIAlertView.您可以使用Cocoapods将其带入:

pod 'HCViews/UIAlertViewHCContext', '~> 1.2'
Run Code Online (Sandbox Code Playgroud)

源代码可在此处获取:https://github.com/hypercrypt/HCViews/blob/master/Categories/UIAlertView%2BHCContext.h


Ant*_*ony 7

很多帖子都谈到了关联对象背后的概念(这很好!)但有时候你只是想看看代码.这里有一个干净,快速的类别,你可以放在一个单独的文件,或将现有的一个接口上面.m的文件(你甚至可以代替UIAlertViewNSObject,有效地增加一个context属性,任何对象):

#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
Run Code Online (Sandbox Code Playgroud)

然后你就可以做类似的事情:

NSObject *myObject = [NSObject new];

UIAlertView *alertView = ...
alertView.context = myObject;
Run Code Online (Sandbox Code Playgroud)

重要提示: 不要忘记在上下文中dealloc取消!!


Phi*_*lls 1

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

  • [UIAlertView Doc](http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertView_Class/UIAlertView/UIAlertView.html) -- _UIAlertView 类旨在按原样使用,而不是支持子类化_ (8认同)