【问题标题】:UIViewController with UIView-inherited class, programmatically具有 UIView 继承类的 UIViewController,以编程方式
【发布时间】:2011-03-07 17:13:44
【问题描述】:

我正在使用一个库,它是一个继承自 UIView 的类。如何以编程方式创建使用此类的 UIViewController,而不是普通的 UIView?

ViewController 的 .h 文件如下所示:

#import <UIKit/UIKit.h>
#import "PLView.h"

@interface HelloPanoramaViewController : UIViewController {
IBOutlet PLView * plView;
}

@property (nonatomic, retain) IBOutlet PLView *plView;

@end

.m文件如下:

#import "HelloPanoramaViewController.h"

@implementation HelloPanoramaViewController

@synthesize plView;

- (void)viewDidLoad 
{
    [super viewDidLoad];
    // Do stuff here...
}

- (void)dealloc 
{
    [plView release];
    [super dealloc];
}

@end

然后我应该使用笔尖让“plView 变量指向视图”。 但是如果不使用 Interface Builder,我将如何以编程方式执行此操作?我怎么能让这个 UIViewController 创建一个 PLView,而不是 UIView?

【问题讨论】:

  • 仅供参考,如果您不知道,您实际上也可以执行此 IB,方法是将 nib 中主 UIView 的“类”属性更改为所需的类名。跨度>

标签: iphone uiview uiviewcontroller


【解决方案1】:

你的 UIViewController 看起来像

#import "HelloPanoramaViewController.h"

@implementation HelloPanoramaViewController

- (void)loadView 
{
    self.view = [PLView plview]//or whatever it takes to create the plview.
}

- (void)viewDidLoad
{
    //create more objects
}

- (void)viewDidUnload
{
    //release unwanted objects that were created viewDidLoad
}

-(void) dealloc
{
    // release all 
    [super dealloc];
}
@end

更多信息...here

【讨论】:

    【解决方案2】:

    在您创建 viewController 的地方,还创建自定义视图的实例,然后将该视图设置为控制器的视图:

    HelloPanoramaViewController *controller = [[HelloPanoramaViewController alloc] init];
    PLView *view = [[PLView alloc] init];
    controller.view = view;
    

    【讨论】:

      猜你喜欢
      • 2012-06-18
      • 1970-01-01
      • 2011-06-08
      • 2017-02-12
      • 1970-01-01
      • 1970-01-01
      • 2013-11-16
      • 2017-07-02
      • 2011-05-07
      相关资源
      最近更新 更多