本文介绍了如何将变量传递给UIAlertView委托?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将变量传递给 UIAlertView
委托?
How do you pass a variable to the UIAlertView
delegate?
我有一个我想在警报视图委托中使用的变量。它仅用于显示 UIAlertView
和 UIAlertView
委托的函数,所以我不认为它应该是控制器上的财产。有没有办法将变量附加到 UIAlertView
并在委托中检索它?
I have a variable that I want to use in the alert view delegate. It is only used in the function that shows the UIAlertView
and the UIAlertView
delegate, so i don't think it should be a property on the controller. Is there a way to attach the variable to UIAlertView
and retrieve it in the delegate?
- (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];
}
}
推荐答案
in界面前的.h:
extern const char MyConstantKey;
@interface ViewController...
in .m import:
in .m import:
import <objc/runtime.h>
实施前.m中的
in .m before implementation
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);
}
in .m alertview callback
in .m alertview callback
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *associatedString = objc_getAssociatedObject(alertView, &MyConstantKey);
NSLog(@"associated string: %@", associatedString);
}
这篇关于如何将变量传递给UIAlertView委托?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!