iOS addSubview 不显示 View

标签 ios objective-c iphone uiview uiviewcontroller

我试图在单击按钮时添加一个 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/

相关文章:

ios - 无法安装 Codename 一个 iOS 调试应用程序

iphone - Objective-C 是创建 iphone 应用程序的强制性要求吗?

ios - 上传图片到服务器

iOS 8 testflight beta 推送通知 token

iphone - 关于使用 NSURLConnection 下载文件并显示进度的问题

ios - 使用 AppDelegate?

ios - 如何快速沿车轮移动车身?

ios - 如何通过我的 Express.js 驱动的 API 将 GridFS 存储文件 (PDF) 最好地提供给连接的客户端 (iOS)?

objective-c - 当与另一个事件重叠时,NSEvents 会通过 NSView

iphone - 如何在我的 iPhone 项目中添加 GData.framework?