我想以编程方式在基于窗口的应用程序中添加一个包含任何内容的新 View ,有人可以告诉我执行此操作的分步过程以及如何以编程方式在该新 View 上添加控件。
我已经创建了一个新 View 并添加了一个标签,现在我如何添加一个按钮,并且当我点击该按钮时我想更改标签的文本? 有人帮助我吗?
最佳答案
在大多数情况下,您使用 ViewController。但是可以将 UIView 直接添加到窗口。也许你可以更多地解释你想做什么。
UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[label setFont:[UIFont boldSystemFontOfSize:18]];
[label setTextAlignment:UITextAlignmentCenter];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setText:@"Hello"];
[view addSubview:label];
[window addSubview:view];
您可以使用
获取窗口的引用(如果您还没有)[[UIApplication sharedApplication] keyWindow];
关于iphone - 如何在 iPhone 中以编程方式在基于窗口的应用程序中添加一个简单的新 UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/801426/