views:

29

answers:

1

Dear scholars, code gurus.

My question is rather simple, I have been searching and trying for some time now without any luck, I would greatly appreciate some tiny help.

I have a simple class that should generate a view as following:

//  vipcardclass.h

//

#import <UIKit/UIKit.h>


@interface vipcardclass : UIViewController {

}
+(IBAction)newsletter;
@end

and

//  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

(Kindly look at the ?????? , few lines above , if it was the main view controller I would use self.view addSubview: , My question is what is the proper syntax when using it from a class)

In my main view controller I #import "vipcardclass.h"

and at some point calling the class function [vipcardclass newsletter];

Which works just fine, apart from adding it to the main controller. I would add as a side note, that the class functionality is not communicating or relevant to any function in the main view controller, its a fire and forget thingy and I just want to open and display this view.

Thank you kindly.

A: 

Try This:

//  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

And in the main View Controller:

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

}

Additionally, you may actually want to subclass UIView and include all the code in newsletter into the init method:

@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

And in the main View Controller:

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

}
Stephen Furlani
Solved and works well , apart from a small case sensative error in your solution [self.view addsubview:vipcard];should be[self.view addSubview:vipcard]; // Capital SKindly change it for the next generations to look at this postThank you for your solution, I appreciate it.
ShiShi
lol, that's what I get for relying on auto-fill.
Stephen Furlani
Hey there Stephen, thank you again for expanding the answer. I am using the first method, and added a button that should remove this view from the superview, but it seems I can't, any ideas regarding the syntax?+(void)closeKeyFunction:(id)sender{ NSLog(@"Close VIP Card %@",sender); //[self.view removeFromSuperview]; // -- the issue is here}
ShiShi
`[self.view` is calling the view you added vipcard to. Try `NSArray *subviews = [self.view subviews];` figure out which view vipcard is, and then `[[subviews objectAtIndex:??] removeFromSuperview];`
Stephen Furlani