无法连接到 Xcode 5 和 Objective-C 中警报和操作表的每个按钮



首先,在下面的头文件中:

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UIActionSheetDelegate>
- (IBAction)btnSheet1:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *myLabel;
-(void) actinSheet:(UIActionSheet *) actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
@end

和实现文件:


#import "ViewController.h"
@implementation ViewController
- (IBAction)btnSheet1:(id)sender 
{
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Action" delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:@"Do it!" otherButtonTitles:@"other button", nil];
    sheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
    [sheet showInView:self.view];
}
-(void) actinSheet:(UIActionSheet *) actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 0)
    {
        self.myLabel.text = [NSString stringWithFormat:@"Button: %d pressed", buttonIndex];
    } 
    else if (buttonIndex == 1)
    {
        self.myLabel.text = [NSString stringWithFormat:@"Button: %d pressed", buttonIndex];
    }
}

@end

当我运行模拟器时,按钮和标签已成功显示在屏幕上。但是,当我单击按钮以弹出操作表,然后尝试按下其中一个按钮以使myLabel更改为其他字符串时,标签没有更改......

我也尝试在警报视图上做同样的事情,但它也没有用。

那么为什么代码不起作用呢?整个片段都来自这本书,这本书假设使用 iOS 6 和 Xcode 4,但我使用 iOS 7 和 Xcode 5,这可能是罪魁祸首?

供您参考,我将标签连接到IBOutlet,并将按钮连接到IBAction,这就是上述书籍的作用。

谢谢。

它不起作用,你写错了委托:

-(void) actinSheet:(UIActionSheet *) actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

应该是actionSheet.

-(void) actionSheet:(UIActionSheet *) actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

最新更新