首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iPhone手工构建的视图/控制器

iPhone手工构建的视图/控制器
EN

Stack Overflow用户
提问于 2009-10-23 00:04:37
回答 3查看 164关注 0票数 1

我想知道是否有人知道通过编程而不是通过接口构建器创建视图和控制器的任何好的在线资源/教程。我看过的所有东西都使用了界面构建器和创建的nib,而IB很好,但我希望可以手动开发它们(出于实际的原因,并且很好地理解了它们是如何结合在一起的,而不是从拖放东西中得到的肤浅的)。

我的背景是Java,我发现使用接口构建器开发视图的速度慢而且令人沮丧,这是我有时在java中所做的事情,即

  • 从域模型以编程方式生成源文件,然后在使用元数据和/或反射并将控件动态添加到视图

的情况下调整结果。

而且,一旦我创建了一个视图,我是否可以将它添加到接口构建器中,以使它可以作为另一个视图上的子视图使用?

谢谢,维克

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-10-23 01:28:25

Interface方法创建“冻结干燥”对象,这些对象在从NIB初始化对象时在运行时重新创建。它仍然执行相同的allocinit操作,使用NSCoder对象将对象带入内存。

如果您想要基于特定NIB的视图控制器,则可以重写默认init方法,并根据该视图控制器的NIB对其进行init。例如:

代码语言:javascript
复制
@implementation MyViewController
-(id) init {
    if (self = [super initWithNibName:@"MyViewController" bundle:nil]) {
        //other setup stuff
    }
    return self;
}

当您想要显示MyViewController时,只需调用以下内容:

代码语言:javascript
复制
- (void) showMyViewController {
    MyViewController *viewController = [[[MyViewController alloc] init] autorelease];
    [self presentModalViewController:viewController animated:YES];
}

现在,如果您希望手动创建视图,而不是在Interface中创建视图,则根本不必更改-showMyViewController方法。去掉您的-init覆盖,而是重写MyViewController-loadView方法,以便以编程方式创建它:

代码语言:javascript
复制
- (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,那么声明它的方式是相同的。例如,您的标题可能如下所示:

代码语言:javascript
复制
@interface MyViewController : UIViewController {
    UIButton *myButton;
}

- (void) pressedButton;

@property (nonatomic, retain) UIButton *myButton;

@end

你的班级:

代码语言:javascript
复制
@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];
}
票数 1
EN

Stack Overflow用户

发布于 2009-10-23 00:08:28

几个月前,当我想在Emacs中完成所有的iPhone开发时,我也遇到了同样的问题。长话短说:我不再为iPhone开发了:)

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

票数 0
EN

Stack Overflow用户

发布于 2009-10-23 00:27:34

我通常不会在iPhone开发中过多地使用接口生成器。通常,我会用如下代码创建一个视图控制器

代码语言:javascript
复制
MyUIViewControllerSubclass *controller = [[MyUIViewControllerSubclass alloc] initWithNibName:nil bundle:nil];
controller.someProperty = myModel;
[self presentModalViewController:controller];
[controller release];

或者其他类似的东西。通常,我创建一个UIViewController的子类,然后在那里布局我的视图等等。视图是UIView的子类(要么是苹果提供的东西,比如UIButton等,要么是我自己创建的)。如果您同时阅读了UIViewControllerUIView,您应该会对它的工作原理有一个很好的了解。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1610716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档