当我从 .NET 过渡到 Objective-C 时,我正在学习控件和事件处理的基础知识。
我使用 ARC 和 Storyboard 在 XCode 4.2 中创建了一个单一视图项目。在 Storyboard 上,ViewController 有一个 UIImageView 和一个 UIButton。UIImageView 在 Identity Inspector 的“标签”条目中被分配了名称“imgLock”。使用相同的技术将按钮命名为“btnToggle”。
视图控制器.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
IBOutlet UIButton *btnToggle;
IBOutlet UIImageView *imgLock;
}
- (IBAction) toggleImage;
@property (readonly, strong) UIButton *btnToggle;
@property (readwrite, strong) UIImageView *imgLock;
@end
视图控制器.m
#import "ViewController.h"
@implementation ViewController
@synthesize btnToggle, imgLock;
BOOL imgState;
- (void) toggleImage {
UIImage *img = [UIImage imageNamed: imgState ? @"Unlocked64" : @"Locked64"];
imgState = !imgState;
[imgLock setImage: img];
}
在情节提要上,我按住 Ctrl 键将蓝线从按钮拖到图像上,然后toggleImage
从生成的上下文菜单中进行选择。
当我在项目中放置一个断点toggleImage
并构建/运行项目时,我看到一切都正常,除了imgLock
0x0。从我的 Windows 时代开始,我想我期待控件被实例化,因为它们被“放置在表单上”。
日志没有注释,除了杀死进程时的警告:警告:函数“void”中“/SourceCache/gdb/gdb-1708/src/gdb/macosx/macosx-nat-inferior.c”的第2184行错误macosx_kill_inferior_safe()"