2010-03-11 21 views
0

嘿傢伙。所以我在loadView中動態地創建按鈕。每個按鈕都使用@Selector方法進行操作,例如:如何在'loadView'以外創建的按鈕方法中更改'self.view'

[button addTarget:self action:@selector(showCCView)forControlEvents:UIControlEventTouchUpInside];

既然showCCView方法是在loadView的上面定義的,那麼就是上述語句的位置。該方法的要點是改變當前在屏幕上的視圖(所以設置self.view = ccView)。它每次嘗試訪問loadView之外的self.view時都會給我一個錯誤,甚至有時當我嘗試在loadView中的隨機位置訪問它時,它的行爲真的很奇怪。

我試圖改變它,所以我不會處理這個。我做了一個函數+(void)showView:(UIView *)oldView:(UIView *)newView;但是這並沒有奏效,因爲@Selector在需要兩個參數的函數中使用它是真正的詭計。

請幫忙嗎?

這裏是我的代碼:

// 
// SiteOneController.m 
// InstantNavigator 
// 
// Created by dni on 2/22/10. 
// Copyright 2010 __MyCompanyName__. All rights reserved. 
// 

#import "SiteOneController.h" 


@implementation SiteOneController 

+ (UIView*) ccContent { 

    UIView *ccContent = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 

    ccContent.backgroundColor = [UIColor whiteColor]; 

    [ccContent addSubview:[SiteOneController myNavBar1:@"Constitution Center Content"]]; 
    return ccContent; 
} 

// Button Dimensions 
int a = 62; 
int b = 80; 
int c = 200; 
int d = 30; 

// NPSIN Green Color 
+ (UIColor*)myColor1 { 
    return [UIColor colorWithRed:0.0f/255.0f green:76.0f/255.0f blue:29.0f/255.0f alpha:1.0f]; 
} 

// Creates Nav Bar with default Green at top of screen with given String as title 
+ (UINavigationBar*)myNavBar1: (NSString*)input { 

    UIView *test = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 
    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, test.bounds.size.width, 45)]; 
    navBar.tintColor = [SiteOneController myColor1]; 

    UINavigationItem *navItem; 
    navItem = [UINavigationItem alloc]; 
    navItem.title = input; 
    [navBar pushNavigationItem:navItem animated:false]; 

    return navBar; 
} 


//-------------------------------------------------------------------------------------------// 
//-------------------------------------------------------------------------------------------// 
//-------------------------------------------------------------------------------------------// 


// Implement loadView to create a view hierarchy programmatically, without using a nib. 
- (void)loadView { 

    //hard coded array of content for each site 

    // CC 
    NSMutableArray *allccContent = [[NSMutableArray alloc] init]; 

    NSString *cc1 = @"House Model"; 
    NSString *cc2 = @"James Dexter History"; 

    [allccContent addObject: cc1]; 
    [cc1 release]; 

    [allccContent addObject: cc2]; 
    [cc2 release]; 

    // FC 
    NSMutableArray *allfcContent = [[NSMutableArray alloc] init]; 

    NSString *fc1 = @"Ghost House"; 
    NSString *fc2 = @"Franklins Letters"; 
    NSString *fc3 = @"Franklins Business"; 

    [allfcContent addObject: fc1]; 
    [fc1 release]; 

    [allfcContent addObject: fc2]; 
    [fc2 release]; 

    [allfcContent addObject: fc3]; 
    [fc3 release]; 

    // PC 
    NSMutableArray *allphContent = [[NSMutableArray alloc] init]; 

    NSString *ph1 = @"Changing Occupancy"; 
    NSString *ph2 = @"Sketches"; 
    NSString *ph3 = @"Servant House"; 
    NSString *ph4 = @"Monument"; 
    NSString *ph5 = @"Virtual Model"; 

    [allphContent addObject: ph1]; 
    [ph1 release]; 

    [allphContent addObject: ph2]; 
    [ph2 release]; 

    [allphContent addObject: ph3]; 
    [ph3 release]; 

    [allphContent addObject: ph4]; 
    [ph4 release]; 

    [allphContent addObject: ph5]; 
    [ph5 release]; 

    // Each content page's view 

    //UIView *ccContent = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 
    UIView *fcContent = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 
    UIView *phContent = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 

    //ccContent.backgroundColor = [UIColor whiteColor]; 
    fcContent.backgroundColor = [UIColor whiteColor]; 
    phContent.backgroundColor = [UIColor whiteColor]; 

    //[ccContent addSubview:[SiteOneController myNavBar1:@"Constitution Center Content"]]; 
    [fcContent addSubview:[SiteOneController myNavBar1:@"Franklin Court Content"]]; 
    [phContent addSubview:[SiteOneController myNavBar1:@"Presidents House Content"]]; 

    //allocate the view 
    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 

    //set the view's background color 
    self.view.backgroundColor = [UIColor whiteColor]; 

    [self.view addSubview:[SiteOneController myNavBar1:@"Sites"]]; 

    NSMutableArray *sites = [[NSMutableArray alloc] init]; 

    NSString *one = @"Constution Center"; 
    NSString *two = @"Franklin Court"; 
    NSString *three = @"Presidents House"; 

    [sites addObject: one]; 
    [one release]; 

    [sites addObject: two]; 
    [two release]; 

    [sites addObject: three]; 
    [three release]; 

    NSString *ccName = @"Constitution Center"; 
    NSString *fcName = @"Franklin Court"; 

    NSString *element; 
    int j = 0; 
    for (element in sites) 
    { 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 

     //setframe (where on screen) 
     //separation is 15px past the width (45-30) 
     button.frame = CGRectMake(a, b + (j*45), c, d); 

     [button setTitle:element forState:UIControlStateNormal]; 

     button.backgroundColor = [SiteOneController myColor1]; 

     /*- (void) fooFirstInput:(NSString*) first secondInput:(NSString*) second { 
      NSLog(@"Logs %@ then %@", first, second); 
     } 
     - (void) performMethodsViaSelectors { 
      [self performSelector:@selector(fooNoInputs)]; 
      [self performSelector:@selector(fooOneInput:) withObject:@"first"]; 
      [self performSelector;@selector(fooFirstInput:secondInput:) withObject:@"first" withObject:@"second"];*/ 

     //UIView *old = self.view; 

     if (element == ccName) { 
      [button addTarget:self action:@selector(showCCView) 
       forControlEvents:UIControlEventTouchUpInside]; 
      } 
      else if (element == fcName) { 
      } 
      else { 
      } 

     [self.view addSubview: button]; 
     j++; 
    } 

} 

// This method show the content views for each of the sites. 
/*+ (void) showCCView { 
    self.view = [SiteOneController ccContent]; 
}*/ 

回答

1

您正在嘗試使用相同的視圖控制器實例來控制多個視圖對象,這是行不通的。如果要轉換爲顯示另一個視圖,則應該使用另一個視圖控制器來顯示它,或至少將新視圖作爲窗口的子視圖添加。

相關問題