使用所有代码的Xcode Cocoa应用程序,没有笔尖

时间:2013-09-28 21:50:55

标签: objective-c xcode cocoa

我想使用Xcode中的所有代码创建一个Cocoa Application ViewController,而不是使用Interface Builder。

我对此进行了大量搜索,但由于大多数人都想使用Interface Builder,因此没有找到任何内容。我在其他地方问过这个问题,但我没有得到任何答案。

请不要说“不要浪费你的时间”,“他们为你做了工作,为什么要自己做?”等等,就像大多数人一样。当我创建应用程序时,我喜欢使用所有代码。

我想使用Objective-C来做到这一点。无论如何,我认为这是唯一的方法。

那么,您能解释一下如何做到这一点或发布链接吗?

1 个答案:

答案 0 :(得分:1)

事实上,许多人更喜欢以编程方式而不是使用Interface Builder创建UI。 - .xib文件与版本控制不兼容。首先,您可以创建一个自定义的UIViewController类,并在App Delegate的应用程序中使用以下内容:didFinishLaunchingWithOptions:method

    CustomViewController* myCustomViewController = [[CustomViewController alloc] init];
    self.window.rootViewController = myCustomViewControllerObject;

您可以在其init,viewDidLoad和其他方法中设置自定义视图控制器类以满足您的需要。要添加大多数界面元素,可以使用适当的方法(例如,对于视图使用initWithFrame)初始化它们,然后将它们添加到viewController的视图中。有很多方法可以做到这一点,但最终您将调用要添加到的UIView的addSubView:方法。要模仿大多数UI元素的IBAction链接,可以调用addTarget:action:forControlEvents:,并实现为action参数传递的选择器。以下是UIViewController的init方法示例:

-(id) init 
{
   (if self = [super init]) {
     UIButton* addSelectedButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
    [addSelectedButton setTitle: @"Some Button Title" forState: UIControlStateNormal];
    addSelectedButton.frame = CGRectMake(sizeForTable.size.width/2 - 100 , sizeForTable.size.width/2 + 40, 200, 40);
    [addSelectedButton addTarget: self action: @selector(addSelectedButtonPressed:) forControlEvents: UIControlEventTouchUpInside];
     [self.view addSubView: addSelectedButton];
}

return self
} 

-(void) addSelectedButtonPressed: (UIEvent*) event 
{
  //do something
}