Objective-C プログラミングを始めたばかりで、インスタンスをルート ビューUISegmentedController
の「サブビュー」として宣言する場所について少し混乱しています。viewController
私はコードを実験してきましたが、「loadView」で作成されたかどうかに関係なく機能viewDidLoad
するinitWithNibName: bundle:
ようです.
階層内のすべてのビューは、プログラムで作成されました。
コード:
UISegmentedControl
どこに配置すればよいかわからないコード:
self.segCon = [[UISegmentedControl alloc]
initWithItems:(NSArray *)@[@"Red",@"Green", @"Blue"]];
self.segCon.frame = CGRectMake(35, 200, 250, 50);
[self.segCon addTarget:self
action:@selector(changeColor:)
forControlEvents:UIControlEventValueChanged];
[self.view addSubview:self.segCon];
BNRHypnosisViewController.m
:
#import "BNRHypnosisViewController.h"
#import "BNRHypnosisView.h"
@interface BNRHypnosisViewController()
@property (strong, nonatomic) UISegmentedControl *segCon;
- (void)changeColor:(id)sender;
@end
@implementation BNRHypnosisViewController
-(void)loadView
{
//create a view
BNRHypnosisView *backgroundView = [[BNRHypnosisView alloc] init];
//set it as *the* view of this view controller
self.view = backgroundView;
UISegmentedControl
ここにコードを配置しますか?
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//Set the tab bar items title
self.tabBarItem.title = @"Hypnotize";
//Create a UIImage from the file
// This will use Hypno@2x.png on retina devices
UIImage *i = [UIImage imageNamed:@"Hypno.png"];
//put the image on the tab bar
self.tabBarItem.image = i;
またはここですか?
}
return self;
}
-(void)viewDidLoad
{
//Always call the super implementation of viewdidload
[super viewDidLoad];
NSLog(@"BNRHypnosisViewController loaded its view");
またはここですか?
}
- (void)changeColor:(id)sender
{
NSLog(@"The Segment controller was touched %d", self.segCon.selectedSegmentIndex);
if(self.segCon.selectedSegmentIndex == 0){
((BNRHypnosisView *)self.view).circleColor = [UIColor redColor];
}
if(self.segCon.selectedSegmentIndex == 1){
((BNRHypnosisView *)self.view).circleColor = [UIColor greenColor];
}
if(self.segCon.selectedSegmentIndex == 2){
((BNRHypnosisView *)self.view).circleColor = [UIColor blueColor];
}
}
@end
フィードバックをお寄せいただきありがとうございます。