将参数发送到AlertView

pdr*_*rod 1 parameters uialertview ios

我有一个委托,它会收到一条消息,删除一个以该项为参数的项目.我想显示确认AlertView,然后,如果用户按是,我想删除它.

所以,我拥有的是

被调用的委托方法:

- (void) deleteRecording:aRecording(Recording*)aRecording {

     NSLog(@"Cancel recording extended view");
     UIAlertView *alert = [[UIAlertView alloc]
                      initWithTitle: NSLocalizedString(@"Cancel recording",nil)
                      message: NSLocalizedString(@"Are you sure you want to cancel the recording?",nil)
                      delegate: self
                      cancelButtonTitle: NSLocalizedString(@"No",nil)
                      otherButtonTitles: NSLocalizedString(@"Yes",nil), nil];
    [alert show];
    [alert release];

}
Run Code Online (Sandbox Code Playgroud)

并且检查按下了哪个按钮的方法:

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

    switch (buttonIndex) {
        case 0:
        {
            NSLog(@"Delete was cancelled by the user");
        }
        break;

        case 1:
        {

            NSLog(@"Delete deleted by user");
        }


    }

}
Run Code Online (Sandbox Code Playgroud)

所以,我的问题是,如何将aRecording参数从第一种方法发送到第二种方法呢?

非常感谢

Kri*_*dra 6

  1. 将该变量存储在成员变量中(最简单的解决方案)
  2. 如果只传递int变量,则可以设置AlertView标记属性.

     myAlertView.tag  = YOUR_INT;
    
    Run Code Online (Sandbox Code Playgroud)
  3. 根据文件,

    注意:UIAlertView类旨在按原样使用,不支持子类化.此类的视图层次结构是私有的,不得修改.

    因此,仅当您不打算将应用程序提交到应用程序商店时,请使用第3种方法.感谢用户soemarko ridwan的提示.

    为了传递复杂对象,子类UIAlertView,添加一个对象属性

    @interface CustomAlertView : UIAlertView
    @property (nonatomic, retain) id object;
    @end
    
    @implementation CustomAlertView
    @synthesize object;
    - (void)dealloc {
        [object release];
        [super dealloc];
    }
    @end
    
    Run Code Online (Sandbox Code Playgroud)

    创建AlertView时

     CustomAlertView *alert = [[CustomAlertView alloc]
                      initWithTitle: NSLocalizedString(@"Cancel recording",nil)
                      message: NSLocalizedString(@"Are you sure you want to cancel the recording?",nil)
                      delegate: self
                      cancelButtonTitle: NSLocalizedString(@"No",nil)
                      otherButtonTitles: NSLocalizedString(@"Yes",nil), nil];
    [alert setObject:YOUR_OBJECT];
    [alert show];
    [alert release];
    
    Run Code Online (Sandbox Code Playgroud)

    在代表中

    - (void)alertView:(TDAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
         NSLog(@"%@", [alertView object]);
    }
    
    Run Code Online (Sandbox Code Playgroud)