标签:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
RootViewController *rootVC = [[RootViewController alloc] init];
self.window.rootViewController = rootVC;
[rootVC release];
[self.window makeKeyAndVisible];
return YES;
}
#import "RootViewController.h"
@interface RootViewController ()
@property (nonatomic, retain) UILabel *label;
@property (nonatomic, retain) UITextField *textField;
@property (nonatomic, retain) UIView *myView;
@end
@implementation RootViewController
- (void)dealloc {
[_myView release];
[super dealloc];
}
//懒加载
- (UILabel *)label {
if (_label == nil) {
_label = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 200, 40)];
_label.text = @"中二洪荒巴达";
_label.textAlignment = NSTextAlignmentCenter;
}
return _label;
}
- (UITextField *)textField {
if (_textField == nil) {
_textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 200, 40)];
_textField.borderStyle = UITextBorderStyleRoundedRect;
_textField.placeholder = @"输入框";
}
return _textField;
}
- (void)viewDidLoad {
[super viewDidLoad];
/*
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"jpg"];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
NSLog(@"filePath is %@",filePath);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 200, 200)];
imageView.image = image;
[self.view addSubview:imageView];
[imageView release];
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 0, 0)];
[self.view addSubview:mySwitch];
[mySwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
[mySwitch release];*/
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"label", @"textField"]];
seg.frame = CGRectMake(50, 100, 275, 40);
[seg addTarget:self action:@selector(segmentedControlAction:) forControlEvents:UIControlEventValueChanged];
// seg.selectedSegmentIndex = 0;
[self.view addSubview:seg];
[seg release];
_myView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, 375, 400)];
[self.view addSubview:_myView];
// Do any additional setup after loading the view.
}
- (void)segmentedControlAction:(UISegmentedControl *)seg {
switch (seg.selectedSegmentIndex) {
case 0:
[[self.myView.subviews firstObject] removeFromSuperview];
[self.myView addSubview:self.label];
break;
case 1:
[[self.myView.subviews firstObject] removeFromSuperview];
[self.myView addSubview:self.textField];
break;
default:
break;
}
}
- (void)switchAction:(UISwitch *)mySwitch {
if (mySwitch.on) {
NSLog(@"开启");
} else {
NSLog(@"关闭");
}
}
标签:
原文地址:http://www.cnblogs.com/wwww543623/p/5171240.html