你如何将一个variables传递给UIAlertView委托?
你如何将一个variables传递给UIAlertView
委托?
我有一个variables,我想在警报视图委托中使用。 它只用于显示UIAlertView
和UIAlertView
委托的function,所以我不认为它应该是控制器上的属性。 有没有办法将variables附加到UIAlertView
并在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]; } }
在界面之前:
extern const char MyConstantKey; @interface ViewController...
in .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 alertviewcallback
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ NSString *associatedString = objc_getAssociatedObject(alertView, &MyConstantKey); NSLog(@"associated string: %@", associatedString); }
使用关联的对象。 这里有更详细的描述: 你的新朋友:Obj-C相关对象
要设置您使用的对象,请使用:
objc_setAssociatedObject(alert, &key, userCode, OBJC_ASSOCIATION_RETAIN);
然后把它拿回来:
SOCode *userCode = objc_getAssociatedObject(alertView, &key);
您还需要添加static char key;
所以这是在飞蛾方法的范围。
更新
我已经把这个包装到了UIAlertView
的类别中。 您可以使用Cocoapods将其带入:
pod 'HCViews/UIAlertViewHCContext', '~> 1.2'
来源可在这里: https : //github.com/hypercrypt/HCViews/blob/master/Categories/UIAlertView%2BHCContext.h
很多post都讨论了关联对象背后的概念(这很好!),但有时候你只是想看看代码。 这里有一个干净而快速的类别,你可以把它放在一个单独的文件中,或者在一个现有的.m
文件的接口上面(你甚至可以用NSObject
代替UIAlertView
,并且可以有效地向任何对象添加一个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
然后,你将能够做到这样的事情:
NSObject *myObject = [NSObject new]; UIAlertView *alertView = ... alertView.context = myObject;
重要提示:不要忘记在dealloc
中dealloc
上下文!
UIAlertView
是UIView
一个子类,它具有可以设置为整数的tag
属性。 不幸的是,如果你需要一个不是整数的东西来标识/传递信息给委托,那么你需要在委托本身上设置一些属性(或者设置一个标签索引的数组)。 Advaith的方式可能会工作,但在技术上不支持苹果。
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];
我怀疑最直接的方式是在警报视图的委托类中的属性。 警报视图没有“用户信息”的任何规定,不支持子分类,这将删除想到的唯一快捷方式。
子类UIAlertView,添加一个名为userInfo属性与您select的types。 在创buildSubclassed UIAlertView实例时设置用户信息值,并从委托方法中检索它。 (在那里你会得到保存userInfo的子类实例)