相关疑难解决方法(0)

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

如何将变量传递给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)

iphone objective-c uialertview ios

24
推荐指数
4
解决办法
2万
查看次数

使用用户提供的上下文和[self autorelease]的UIAlertView

我已经查看了如何为UIAlertView提供上下文的一些想法.常见的答案是将其保存在字典或子类UIAlertView中.我不喜欢在字典中保存上下文的想法,这是数据的错误位置.Apple不支持对UIAlertView进行子类化,因此根据我的标准,这不是一个好的解决方案.

我提出了一个想法,但我不知道该怎么做.创建一个上下文对象的实例,该对象是UIAlertView的委托.反过来,警报视图上下文有自己的委托,它是视图控制器.

麻烦在于释放记忆.我将alertView.delegate设置为nil并调用[self autorelease]以释放-alertView中的上下文对象:didDismissWithButtonIndex:.

问题是:我自己造成了什么问题?我怀疑自己正在为一个微妙的内存错误做好准备.

这是一个只支持-alertView的简单版本:clickedButtonAtIndex:

使用

- (void)askUserIfTheyWantToSeeRemoteNotification:(NSDictionary *)userInfo
{
    [[[[UIAlertView alloc] initWithTitle:[userInfo valueForKey:@"action"]
                                 message:[userInfo valueForKeyPath:@"aps.alert"]
                                delegate:[[WantAlertViewContext alloc] initWithDelegate:self context:userInfo]
                       cancelButtonTitle:@"Dismiss"
                       otherButtonTitles:@"View", nil] autorelease] show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex withContext:(id)context
{
    if (buttonIndex != alertView.cancelButtonIndex)
        [self presentViewForRemoteNotification:context];
}
Run Code Online (Sandbox Code Playgroud)

接口

@protocol WantAlertViewContextDelegate <NSObject>
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex withContext:(id)context;
@end

@interface WantAlertViewContext : NSObject <UIAlertViewDelegate>
- (id)initWithDelegate:(id<WantAlertViewContextDelegate>)delegate context:(id)context;
@property (assign, nonatomic) id<WantAlertViewContextDelegate> delegate;
@property (retain, nonatomic) id context;
@end
Run Code Online (Sandbox Code Playgroud)

履行

@implementation WantAlertViewContext
- (id)initWithDelegate:(id<WantAlertViewContextDelegate>)delegate context:(id)context
{
    self = [super …
Run Code Online (Sandbox Code Playgroud)

objective-c uialertview ios

2
推荐指数
1
解决办法
2044
查看次数

标签 统计

ios ×2

objective-c ×2

uialertview ×2

iphone ×1