简体   繁体   中英

How can i load a different view on same method in iphone

First Class

@implementation WatchViewController

- (void)viewDidLoad
{
    [super viewDidLoad];UIButton *watch1 = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
    watch1.frame = CGRectMake(5.0, 10.0, 140.0, 170.0);
    [watch1 setBackgroundImage:[UIImage imageNamed:@"Watch1.png"] forState: UIControlStateNormal];
    [watch1 addTarget:self action:@selector(WatchesPreviewButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [scr addSubview:watch1];

    UIButton *watch2 = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
    watch2.frame = CGRectMake(170.0, 10.0, 140.0, 170.0);
    [watch2 setBackgroundImage:[UIImage imageNamed:@"watch2.png"] forState: UIControlStateNormal];
    [watch2 addTarget:self action:@selector(WatchesPreviewButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [scr addSubview:watch2];
}

Method:

- (IBAction)WatchesPreviewButtonPressed:(id)sender {

    WatchPreviewViewController *watchesPreviewView = [[WatchPreviewViewController alloc] initWithNibName:@"WatchPreviewViewController"  bundle:nil];
    [self.navigationController pushViewController:watchesPreviewView animated:YES];
    [watchesPreviewView release];
}

Second Class:

@implementation WatchPreviewViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
UIScrollView *scr=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 46, 320, 384)];
[self.view addSubview:scr];
NSArray* ds =[NSArray arrayWithObjects:
                      [NSArray arrayWithObjects:[self getPath:@"1a"],[self getPath:@"1b"],[self getPath:@"1c"],[self getPath:@"1d"],nil],
                      [NSArray arrayWithObjects:[self getPath:@"2a"],[self getPath:@"2b"],[self getPath:@"2c"],[self getPath:@"2d"],nil],nil];


 SSView* sv =[SSView createWithFrame:CGRectMake(0, 0, 320, 380) ds:ds];

    if(??????????????????)   //what condition is required for watch1?
{
        sv.curIndexPath =[NSIndexPath indexPathForRow:0 inSection:0];
        [scr addSubview:sv];
}
    else if(?????????????????)          //what condition is required watch2?
{
        sv.curIndexPath =[NSIndexPath indexPathForRow:1 inSection:0];
        [scr addSubview:sv];
}

In class one i have two images of watch and i want load next page view on the click of watch. for this i am using method WatchesPreviewButtonPressed . In second Class i am creating the page for loading on button click. in second class i have a scroll view inside the view. and i have array for images. i want display different image on next watch click event. any one please me, i am new in iphone development.

Create an enum like style in WatchPreviewViewController and create your own init method.

typedef enum WatchPreviewViewControllerStyleType {
    WatchPreviewViewControllerStyleType1 = 0,
    WatchPreviewViewControllerStyleType2 = 1
    }WatchPreviewViewControllerStyleType;

@interface WatchPreviewViewController : UIViewController
{
    WatchPreviewViewControllerStyleType style;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andStyle:(WatchPreviewViewControllerStyleType)_style;
@implementation


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andStyle:(WatchPreviewViewControllerStyleType)_style
{
    self.style=_style;
}

- (void)viewDidLoad
{
    [super viewDidLoad];


    if(self.style==WatchPreviewViewControllerStyleType1)   
   {

   }
    else if(self.style==WatchPreviewViewControllerStyleType2) 
   {
   }
}

in this custom init set the ivar style sent while creating the controller. and then in viewDidload check for the style type and add the required views for that style.

and in WatchViewController

@implementation WatchViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
  UIButton *watch1 = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
   watch1.tag=123;
    watch1.frame = CGRectMake(5.0, 10.0, 140.0, 170.0);
    [watch1 setBackgroundImage:[UIImage imageNamed:@"Watch1.png"] forState: UIControlStateNormal];
    [watch1 addTarget:self action:@selector(WatchesPreviewButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [scr addSubview:watch1];

    UIButton *watch2 = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
       watch1.tag=456;
    watch2.frame = CGRectMake(170.0, 10.0, 140.0, 170.0);
    [watch2 setBackgroundImage:[UIImage imageNamed:@"watch2.png"] forState: UIControlStateNormal];
    [watch2 addTarget:self action:@selector(WatchesPreviewButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [scr addSubview:watch2];
}

- (IBAction)WatchesPreviewButtonPressed:(id)sender {

if(sender.tag==123)
{
   WatchPreviewViewController *watchesPreviewView = [[WatchPreviewViewController alloc] initWithNibName:@"WatchPreviewViewController"  bundle:nil andStyle:WatchPreviewViewControllerStyleType1];
    [self.navigationController pushViewController:watchesPreviewView animated:YES];
    [watchesPreviewView release];
}
else
{
   WatchPreviewViewController *watchesPreviewView = [[WatchPreviewViewController alloc] initWithNibName:@"WatchPreviewViewController"  bundle:nil andStyle:WatchPreviewViewControllerStyleType2] ;
    [self.navigationController pushViewController:watchesPreviewView animated:YES];
    [watchesPreviewView release];
}

}

Set the tag property on each button when you create them in (void)viewDidLoad :

watch1.tag = 1    //new code
[scr addSubview:watch1];  //existing code


watch2.tag = 2  //new code
[scr addSubview:watch2];  //existing code

In your WatchPreviewViewController.h , make a property in the @interface section:

@property (assign) int watchType;

Then in - (IBAction)WatchesPreviewButtonPressed:(id)sender set the property according to which button is pressed:

watchesPreviewView.watchType = sender.tag

(you might have to typecast sender: (UIView*)sender.tag , I am not testing this live)

Now your if(??????????????????) test is if (self.watchType == 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM