首先,在以下头文件中:
#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,这就是上述书籍的功能。
谢谢。