2

我刚刚开始使用 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    

任何帮助将不胜感激,提前感谢您的反馈!

4

2 回答 2

3

我通常把这样的代码放在viewDidLoad. 但是由于您需要实现loadView,您可以在那里设置分段控件。

不要在init...方法中这样做,因为那样你最终会不必要地从init...方法中加载视图。

于 2014-05-18T16:19:33.800 回答
0

我认为最好的做法是通过不实例化任何不需要的东西来节省应用程序在任何给定时间使用的内存。换句话说,正如@rmaddy 所说,您不想在init视图控制器的方法中调用会过早创建用户不需要的视图并因此占用堆上宝贵内存的方法。按照这个前提,你不应该出错。不错的帖子顺便说一句。我正在关注 Big Nerd Ranch 的同一本书,对于任何查看此书的初学者来说,它都是一个很好的学习工具!附带说明一下,我看到您UIColor circleColor;BNRHypnosisView.h. 您可以将其保留为类扩展,并仍然通过使用如下键值编码 _circleColor在您的方法中设置实例变量:- (void)changeColor:(id)sender

    - (void)changeColor:(id)sender
{
    NSLog(@"The segment controller was touched %d", self.colorSwitch.selectedSegmentIndex);
    if (self.colorSwitch.selectedSegmentIndex == 0) {
        [self.view setValue:[UIColor redColor] forKeyPath:@"circleColor"];
    } else if (self.colorSwitch.selectedSegmentIndex == 1) {
        [self.view setValue:[UIColor greenColor] forKeyPath:@"circleColor"];
    } else if (self.colorSwitch.selectedSegmentIndex == 2) {
        [self.view setValue:[UIColor blueColor] forKeyPath:@"circleColor"];
   }
}
于 2014-07-27T00:26:03.107 回答