1 iphone objective-c uibutton ios
有一个AppDelegate和一个MainViewController.在MainViewController将UIButton被创建,AppDelegate设置背景图片,并应表现出它MainViewController.这几乎是它,但我看不到UIButton.
为了对齐目的,我IBOutlet在IB中创建并连接了按钮.
MainViewController.h
@property (strong, nonatomic) IBOutlet UIButton *searchBtn;
Run Code Online (Sandbox Code Playgroud)
MainViewController.m
@synthesize searchBtn;
Run Code Online (Sandbox Code Playgroud)
AppDelegate.m
@synthesize mainVC;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ViewController > setup:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.mainVC = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
self.window.rootViewController = self.mainVC;
self.window.backgroundColor = [UIColor whiteColor];
// Buttons > Set background images:
[self.mainVC.searchBtn setImage:[UIImage imageNamed:@"search.png"] forState:UIControlStateNormal];
[self.mainVC.view addSubview:self.mainVC.searchBtn];
[self.window makeKeyAndVisible];
return YES;
}
Run Code Online (Sandbox Code Playgroud)
首先,IBOutlet如果您以编程方式创建它,则不需要.
其次,您可能希望在viewDidLoad视图控制器中而不是在您的视图控制器中创建按钮AppDelegate.这是您的视图控制器的业务.
第三,alloc init如果你以编程方式创建它,你可能应该是你的按钮:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(...)];
[button setBackgroundImage:someImage];
[button addTarget:self action:@selector(something:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
Run Code Online (Sandbox Code Playgroud)
类似的东西.
| 归档时间: |
|
| 查看次数: |
2122 次 |
| 最近记录: |