简体   繁体   English

如何在iPhone的相同方法上加载不同的视图

[英]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 . 为此,我正在使用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. 任何人都请我,我是iPhone开发的新手。

Create an enum like style in WatchPreviewViewController and create your own init method. 在WatchPreviewViewController中创建一个类似于样式的枚举,然后创建自己的init方法。

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. 在此自定义init中,设置在创建控制器时发送的ivar样式。 and then in viewDidload check for the style type and add the required views for that style. 然后在viewDidload中检查样式类型,并添加该样式所需的视图。

and in WatchViewController 并在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 : (void)viewDidLoad创建按钮时,在每个按钮上设置tag属性:

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: 在您的WatchPreviewViewController.h ,在@interface部分中创建一个属性:

@property (assign) int watchType;

Then in - (IBAction)WatchesPreviewButtonPressed:(id)sender set the property according to which button is pressed: 然后在- (IBAction)WatchesPreviewButtonPressed:(id)sender根据按下的按钮设置属性:

watchesPreviewView.watchType = sender.tag

(you might have to typecast sender: (UIView*)sender.tag , I am not testing this live) (您可能必须强制转换发件人: (UIView*)sender.tag ,我没有对此进行实时测试)

Now your if(??????????????????) test is if (self.watchType == 1) 现在您的if(??????????????????)测试是if (self.watchType == 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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