在insertnewObject中的UIAlertView委托

时间:2013-12-11 05:42:05

标签: iphone ios7 uialertview

我在insertnewobject函数中有一个uialertview,如下所示。

- (void)insertNewObject:(UIButton *)sender
{
    //NSLog(@"Hello");
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Add Reference"
                                                      message:@"Enter Reference Manually"
                                                     delegate:nil
                                            cancelButtonTitle:@"Insert Manually"
                                            otherButtonTitles:nil];
    [message addButtonWithTitle:@"Enter ISBN"];
    [message addButtonWithTitle:@"Enter DOI"];
    [message show];
}

我可以看到UIAlertView。但是根本没有达到这个功能!!

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"Hello");
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"Enter ISBN"])
    {
        NSLog(@"Button 1 was selected.");
    }
    else if([title isEqualToString:@"Enter DOI"])
    {
        NSLog(@"Button 2 was selected.");
    }
    else if([title isEqualToString:@"Insert Manually"])
    {
        NSLog(@"Button 3 was selected.");
    }
}

请告诉我哪里出错了!

4 个答案:

答案 0 :(得分:2)

将委托设置为自我

更新此行

UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Add Reference"
                                                  message:@"Enter Reference Manually"
                                                 delegate:self
                                        cancelButtonTitle:@"Insert Manually"
                                        otherButtonTitles:nil];

答案 1 :(得分:1)

使用title proprty,你可以直接使用buttonIndex属性,并将委托设置为self,

试试这个

 UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Add Reference"
                                                          message:@"Enter Reference Manually"
                                                         delegate:self
                                                cancelButtonTitle:@"Insert Manually"
                                                otherButtonTitles:nil];
        [message addButtonWithTitle:@"Enter ISBN"];
        [message addButtonWithTitle:@"Enter DOI"];
        [message show];

Alertview委派方法:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"Hello");

    if(buttonIndex==1)
    {
        NSLog(@"Button 1 was selected.");
    }
    else if(buttonIndex==2)
    {
        NSLog(@"Button 2 was selected.");
    }
    else if(buttonIndex==0)
    {
        NSLog(@"Button 3 was selected.");
    }
}

答案 2 :(得分:1)

您设置delegate nil,delegate应该是self来调用委托方法。

答案 3 :(得分:0)

您已设置delegate:nil然后如何调用委托方法。只需将其设置为delegate:self即可看到魔法。

 UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Add Reference"
                                                  message:@"Enter Reference Manually"
                                                 delegate:self //just change here 
                                        cancelButtonTitle:@"Insert Manually"
                                        otherButtonTitles:nil];