【问题标题】:Where to add the UISegmentedController; loadView, initWithNibName: bundle:, or viewDidLoad?在哪里添加 UISegmentedController; loadView、initWithNibName: bundle: 还是 viewDidLoad?
【发布时间】:2014-05-18 16:13:19
【问题描述】:

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

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

【问题讨论】:

  • 旁注 - 如果您以编程方式做所有事情(我自己做),那么您为什么要实现 initWithNibName:bundle: 方法?只需执行init
  • 这是个好问题。我实际上正在关注“iOS 编程 - Big Nerd Ranch 指南”一书,这就是它的指示。在我创建这个视图控制器的 appDelegate 中,我使用了 init。我认为因为 initwithNibName... 是指定的初始化程序,所以无论如何都会调用它,并且 nibName 和 bundle 的参数都是 nil 。话虽这么说,我想我可能只是重写了 init 而不是隐式调用 initWithNibName ...
  • 是的,因为你没有使用笔尖,只需覆盖init。它更简单、更干净。
  • 我使用了本书的前一版本,这让我使用了界面构建器(.xib 文件)来创建一些视图控制器。
  • @SantaClaus 是的,3 个 viewController 中有 2 个使用界面构建器。但是我发布的那个没有。也许它是为了一致性而写的..?

标签: ios objective-c uisegmentedcontrol viewdidload loadview


【解决方案1】:

我通常将此类代码放在viewDidLoad 中。但是由于你需要实现loadView,你可以在那里设置分段控件。

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

【讨论】:

  • 感谢您的回复(不能投票给您,因为我需要 15 名声望,但一旦获得,我会的!)。您是否还建议我将 if (self) { self.tabBarItem.title = @"Hypnotize"; UIImage *i = [UIImage imageNamed:@"Hypno.png"]; self.tabBarItem.image = i; 放在 loadView 或 viewDidLoad 方法中?为什么你认为这本书指示我把这个放在 init 方法中?
  • init 中设置tabBarItem 很好。只需在 init 方法中避免 self.view
  • 好的,谢谢。在 init 方法中设置 tabBarItem 和类似设置是否被认为是最佳做法?
  • 并非如此。设置tabBarItem 可以在initviewDidLoad 中完成,甚至可以在创建视图控制器的调用代码中完成。在这种情况下,答案是“视情况而定”。
  • 它取决于什么?如果在 init 或 viewDidLoad 中设置,执行代码会有什么不同吗?再次感谢
【解决方案2】:

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

    - (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"];
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    • 2011-12-13
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多