2009-10-23 99 views
1

我想知道是否有人以编程方式而不是通过界面构建​​器知道任何良好的在线资源/教程来创建视图和控制器。我看过的所有东西都使用界面构建器和创建的笔尖,而IB是可以的,我希望可以选择手动开发这些东西(两者都是出于实际的原因,并且很好地理解了它们如何融合在一起而不是表面化的你从拖放东西中获得的东西)。iPhone手工制作视图/控制器

我的背景是在Java中,我使用界面生成器发展的意见的方式我会发现它缓慢,令人沮丧,有时做他们在Java中,即或者

  • 编程从域生成源模型,然后调整的结果,如果requried
  • 使用一些元数据和/或反射和动态添加控制到视图

另外,一旦我已经创建了一个视图是有反正我可以将其添加到接口生成器到ma可以将它用作另一个视图的子视图吗?

感谢,维克

回答

1

当您从NIB初始化对象时,Interface Builder方法会创建在运行时重新创建的“冻干”对象。它仍然使用相同的allocinit东西,使用NSCoder对象将对象带入内存。

如果你想拥有一个基于特定NIB的视图控制器,那么你可以重写默认的init方法,并根据该视图控制器的NIB进行初始化。例如:

@implementation MyViewController 
-(id) init { 
    if (self = [super initWithNibName:@"MyViewController" bundle:nil]) { 
     //other setup stuff 
    } 
    return self; 
} 

而当你想要显示的MyViewController,你只需调用是这样的:

- (void) showMyViewController { 
    MyViewController *viewController = [[[MyViewController alloc] init] autorelease]; 
    [self presentModalViewController:viewController animated:YES]; 
} 

现在,如果你想创建在Interface Builder视图,无需手动,你根本不需要改变你的-showMyViewController方法。摆脱你的-init覆盖的,而是覆盖你MyViewController-loadView方法以编程方式创建它:

- (void) loadView { 
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(320,460)]; 
    self.view = view; 
    [view release]; 

    //Create a button 
    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [myButton addTarget:self action:@selector(pressedButton) forControlEvents:UIControlEventTouchUpInside]; 
    [myButton setTitle:@"Push Me!" forState:UIControlStateNormal]; 
    myButton.frame = CGRectMake(100,230,80,44); 
    [self.view addSubview:myButton]; 
} 

这个例子说明如何创建视图和一个按钮添加到它。如果您想保留对它的引用,请按照与使用NIB(不使用IBOutlet/IBActions)相同的方式进行声明,并在分配时使用self。例如,你的头可能是这样的:

@interface MyViewController : UIViewController { 
    UIButton *myButton; 
} 

- (void) pressedButton; 

@property (nonatomic, retain) UIButton *myButton; 

@end 

而且你的类:

@implementation MyViewController 
@synthesize myButton; 

- (void) loadView { 
    //Create the view as above 

    self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [myButton addTarget:self action:@selector(pressedButton) forControlEvents:UIControlEventTouchUpInside]; 
    [myButton setTitle:@"Push Me!" forState:UIControlStateNormal]; 
    myButton.frame = CGRectMake(100,230,80,44); 
    [self.view addSubview:myButton]; 
} 

- (void) pressedButton { 
    //Do something interesting here 
    [[[[UIAlertView alloc] initWithTitle:@"Button Pressed" message:@"You totally just pressed the button" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK",nil] autorelease] show]; 
} 

- (void) dealloc { 
    [myButton release]; 
    [super dealloc]; 
} 
+0

谢谢你,这是非常有帮助的。 – vickirk 2009-10-23 14:13:02

0

我几个月前有同样的问题,当我想要做的Emacs中所有的iPhone开发。长话短说:我不再为iPhone开发了:)

我仍然建议你检查我的问题和一些有用的答案here

+0

他不是问一个开发环境,而它的方式来建立自己的观点(编程或图形)。与文本编辑器无关。 – jbrennan 2009-10-23 00:23:25

0

我通常不会为iPhone开发太多地使用接口构建器。通常我会在这样的代码中创建一个视图控制器

MyUIViewControllerSubclass *controller = [[MyUIViewControllerSubclass alloc] initWithNibName:nil bundle:nil]; 
controller.someProperty = myModel; 
[self presentModalViewController:controller]; 
[controller release]; 

或者沿着这些线。通常我会创建一个UIViewController的子类,这是我布置我的视图等的地方。这些视图是UIView的子类(Apple提供的东西就像UIButton等等,或者我自己创建的东西)。如果你对UIViewControllerUIView都有所了解,你应该清楚它的工作原理。