我试图在单击按钮时添加一个 subview 。但是当点击按钮时, View 并没有出现。
这是点击按钮的非常简单的代码:
-(IBAction) dimChangeBtnClick: (id) sender
{
[self addSubview:myHelloWorldViewController.view];
}
myHelloWorldViewController 是 HelloWorldViewController 类的实例。
HelloWorldViewController.h:
#import <UIKit/UIKit.h>
@interface HelloWorldViewController : UIViewController
-(IBAction)showMessage;
@end
和 HelloWorldViewController.m:
#import <Foundation/Foundation.h>
#import "HelloWorldViewController.h"
@implementation HelloWorldViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
-(IBAction)showMessage
{
UIAlertView *hellowWorldAlert = [[UIAlertView alloc] initWithTitle:@"My First App" message:@"Hello, World!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[hellowWorldAlert show];
}
@end
我要展示的 View 是 HelloWorldViewController.xib
除了相同的命名之外,Objective-C 代码和 .xib 文件之间是否还需要一些额外的链接?从我的二年级问题中可以看出,我对 iOS 很陌生,所以任何建议都将不胜感激。如果我遗漏了任何关键信息,请告诉我。
最佳答案
如果您想将来自另一个 UIViewController 的 View 嵌入到您的主视图 Controller 中,则需要一些过程才能将该 View 的某些控制权正确地转移到主视图 Controller 。这在 these Apple docs 中有详细描述。
如果您更喜欢教程式的演练,那么您可以 try this one
关于iOS addSubview 不显示 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38516838/