将一个简单的UIView从类添加到主ViewController

时间:2010-08-18 14:52:37

标签: iphone objective-c class uiview uiviewcontroller

亲爱的学者,代码大师。

我的问题很简单,我一直在寻找和尝试一段时间没有任何运气,我会非常感谢一些小小的帮助。

我有一个简单的类应该生成如下视图:

//  vipcardclass.h

//

#import <UIKit/UIKit.h>


@interface vipcardclass : UIViewController {

}
+(IBAction)newsletter;
@end

//  vipcardclass.m

#import "vipcardclass.h"


@implementation vipcardclass



+(IBAction)newsletter{


    UIView *vipcard = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 290.0, 280.0)];
    UIImageView *vipcardBG = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"vip_card.png"]]; //background.png
    [vipcard addSubview:vipcardBG];     



    CGRect webFrame = CGRectMake(6.0, 0.0, 300.0, 210.0); 
    UIWebView *content = [[UIWebView alloc] initWithFrame:webFrame]; 
    [content setBackgroundColor:[UIColor clearColor]];
    [content setOpaque:NO];
    NSString *urlAddress = @"http://www.google.com";
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
    [content loadRequest:requestObj]; 
    [vipcard addSubview:content]; 
    [content release]; 

    [???????? addSubview:vipcard];
    NSLog(@"Newsletter was called");




}
@end

(请看看??????,上面几行,如果是主视图控制器我会使用self.view addSubview:,我的问题是从类中使用它时的正确语法是什么)

在我的主视图控制器中  #import“vipcardclass.h”

并在某些时候调用类函数 [vipcardclass newsletter];

除了将其添加到主控制器之外,其工作正常。 我会补充说,类功能不与主视图控制器中的任何功能进行通信或相关,它是一个火,忘记了东西,我只是想打开并显示这个视图。

谢天谢地。

1 个答案:

答案 0 :(得分:0)

试试这个:

//  vipcardclass.m

#import "vipcardclass.h"


@implementation vipcardclass



+ (UIView*) newsletter {


    UIView *vipcard = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 290.0, 280.0)];
    UIImageView *vipcardBG = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"vip_card.png"]]; //background.png
    [vipcard addSubview:vipcardBG];     
    [vipcardBG release];


    CGRect webFrame = CGRectMake(6.0, 0.0, 300.0, 210.0); 
    UIWebView *content = [[UIWebView alloc] initWithFrame:webFrame]; 
    [content setBackgroundColor:[UIColor clearColor]];
    [content setOpaque:NO];
    NSString *urlAddress = @"http://www.google.com";
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
    [content loadRequest:requestObj]; 
    [vipcard addSubview:content]; 
    [content release]; 

    NSLog(@"Newsletter was called");

    return [vipcard retain];



}
@end

在主视图控制器中:

- (IBAction) loadNewsletter
{
    UIView *vipcard = [vipcardclass newsletter];
    [self.view addSubview:vipcard];
    [vipcard release];

}

此外,您可能实际上想要将UIView子类化并将newsletter中的所有代码包含到init方法中:

@interface vipcardclass : UIView {

}
@end

@implementation vipcardclass

 - (id) init {
    self = [super init];
    if (self != nil) {

        UIImageView *vipcardBG =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"vip_card.png"]];
   //background.png
       [self addSubview:vipcardBG];     
       [vipcardBG release];


       CGRect webFrame = CGRectMake(6.0, 0.0, 300.0, 210.0); 
       UIWebView *content = [[UIWebView alloc] initWithFrame:webFrame]; 
       [content setBackgroundColor:[UIColor
   clearColor]];
       [content setOpaque:NO];
       NSString *urlAddress = @"http://www.google.com";
       NSURL *url = [NSURL URLWithString:urlAddress];
       NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
       [content loadRequest:requestObj]; 
       [self addSubview:content]; 
       [content release]; 

       NSLog(@"Newsletter was called");
    }
    return self;
}

@end

在主视图控制器中:

- (IBAction) loadNewsletter
{
    UIView *vipcard = [[vipcardclass alloc] init];
    [self.view addSubview:vipcard];
    [vipcard release];

}