我正在学习如何在iPhone上开发,我买了一本名为Beginning iPhone 3开发探索SDK的书。在我咬了之后我决定抛弃Interface Builder。我仍然在IB中设计我的所有视图,但是我在代码中编写了所有视图,并且仅使用nib文件来获取控件的帧。

所以现在我需要创建一个 UIButton ,并且文档与其他控件不同。我尝试使用 initWithFrame:,并且这个其他方法 buttonWithType:我认为是自动释放的,但无论如何我无法在屏幕上显示按钮。有人可以写一些代码,在本地创建一个标题我可以更改的按钮,然后我可以添加到我的视图子视图和发布所以我可以看到它是如何完成的?

有帮助吗?

解决方案

我会尝试这样的事情:

    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    myButton.frame = CGRectMake(20, 20, 200, 44); // position in the parent view and set the size of the button
    [myButton setTitle:@"Click Me!" forState:UIControlStateNormal];
    // add targets and actions
    [myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    // add to a view
    [superView addSubview:myButton];

免责声明:只需在此输入即可。我目前无法访问我的Mac,所以我无法测试它。

P.S。有什么特别的理由不使用Interface Builder?好奇。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top