簡體   English   中英

如何在iPhone的相同方法上加載不同的視圖

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

頭等艙

@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];
}

方法:

- (IBAction)WatchesPreviewButtonPressed:(id)sender {

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

二等艙:

@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];
}

在第一堂課中,我有兩張手表圖像,我想在點擊手表時加載下一頁視圖。 為此,我正在使用WatchesPreviewButtonPressed方法。 在第二堂課中,我將創建用於單擊按鈕加載的頁面。 在第二節課中,我在視圖內有一個滾動視圖。 而且我有圖像數組。 我想在下次觀看點擊事件時顯示不同的圖像。 任何人都請我,我是iPhone開發的新手。

在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) 
   {
   }
}

在此自定義init中,設置在創建控制器時發送的ivar樣式。 然后在viewDidload中檢查樣式類型,並添加該樣式所需的視圖。

並在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];
}

}

(void)viewDidLoad創建按鈕時,在每個按鈕上設置tag屬性:

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


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

在您的WatchPreviewViewController.h ,在@interface部分中創建一個屬性:

@property (assign) int watchType;

然后在- (IBAction)WatchesPreviewButtonPressed:(id)sender根據按下的按鈕設置屬性:

watchesPreviewView.watchType = sender.tag

(您可能必須強制轉換發件人: (UIView*)sender.tag ,我沒有對此進行實時測試)

現在您的if(??????????????????)測試是if (self.watchType == 1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM