2011-06-15 54 views
1

我有一個非常有趣的問題,涉及loadView,viewDidLoad,viewWillAppear和伊娃的狀態。我有一個基於導航的應用程序。從我的第一層次看,我有一個表格列表,然後點擊其中一個單元格,它會將我帶到二級視圖(細節視圖)。當我點擊一個單元格時,我還向被推入的視圖控制器添加了一個「Office」對象(其中包含像streetAddressboxAddress這樣的字符串)。然後,我使用Office對象的內容填充詳細視圖,如[box setText:[self.office boxAddress]];(框是UILabel)。現在我想在這裏實現的是,有時boxAddress的stringValue是空的,在這種情況下,我不想向UILabel添加空字符串,而是要將下一個UILabel向上移動(並取代boxAddress)。因此,我已經做了一個有條件檢查,看看boxAddress是否爲空,如果它應該設置爲UILabel s與特定的座標,如果它不是空的,它應該設置UILabel s與其他特定的座標。iOS構建視圖以編程方式取決於對象ivar

我知道你應該使用viewWillAppear如果你希望每次加載視圖時運行代碼。但由於某種原因,viewWillAppear似乎只在boxAddress字符串爲空時纔會運行。如果我點擊一個空格爲boxAddress的單元格,它將使用我點擊的最後一個單元格的值爲boxAddress,該單元格的空格爲非空的boxAddress

我會在這裏粘貼我的代碼,看看你是否可以給我一個指針,說明我在這裏做錯了什麼。

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

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

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

- (void)viewDidLoad 
{ 

    //add the labels 
    name = [[UILabel alloc] initWithFrame:CGRectMake(10.0,10.0,320.0,20.0)]; 
    [name setBackgroundColor:[UIColor clearColor]]; 
    [name setTextColor:[UIColor blackColor]]; 

    street = [[UILabel alloc] initWithFrame:CGRectMake(10.0,30.0,320.0,20.0)]; 
    [street setBackgroundColor:[UIColor clearColor]]; 
    [street setTextColor:[UIColor blackColor]]; 

    //if no box address, move up the rest of the addresses 
    if ([[self.office boxAddress] length] == 0) { 

     zip = [[UILabel alloc] initWithFrame:CGRectMake(10.0,50.0,320.0,20.0)]; 
     [zip setBackgroundColor:[UIColor clearColor]]; 
     [zip setTextColor:[UIColor blackColor]]; 

     phone = [[UILabel alloc] initWithFrame:CGRectMake(10.0,70.0,320.0,20.0)]; 
     [phone setBackgroundColor:[UIColor clearColor]]; 
     [phone setTextColor:[UIColor blackColor]]; 

     fax = [[UILabel alloc] initWithFrame:CGRectMake(10.0,90.0,320.0,20.0)]; 
     [fax setBackgroundColor:[UIColor clearColor]]; 
     [fax setTextColor:[UIColor blackColor]]; 

     [self.view addSubview:name]; 
     [self.view addSubview:street]; 
     [self.view addSubview:zip]; 
     [self.view addSubview:phone]; 
     [self.view addSubview:fax]; 

    } else { 

     box = [[UILabel alloc] initWithFrame:CGRectMake(10.0,50.0,320.0,20.0)]; 
     [box setBackgroundColor:[UIColor clearColor]]; 
     [box setTextColor:[UIColor blackColor]]; 

     zip = [[UILabel alloc] initWithFrame:CGRectMake(10.0,70.0,320.0,20.0)]; 
     [zip setBackgroundColor:[UIColor clearColor]]; 
     [zip setTextColor:[UIColor blackColor]]; 

     phone = [[UILabel alloc] initWithFrame:CGRectMake(10.0,90.0,320.0,20.0)]; 
     [phone setBackgroundColor:[UIColor clearColor]]; 
     [phone setTextColor:[UIColor blackColor]]; 

     fax = [[UILabel alloc] initWithFrame:CGRectMake(10.0,110.0,320.0,20.0)]; 
     [fax setBackgroundColor:[UIColor clearColor]]; 
     [fax setTextColor:[UIColor blackColor]]; 

     [self.view addSubview:name]; 
     [self.view addSubview:street]; 
     [self.view addSubview:box]; 
     [self.view addSubview:zip]; 
     [self.view addSubview:phone]; 
     [self.view addSubview:fax]; 
    } 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
} 

// viewWillAppear method is run every time the view is loaded as opposite to the viewDidLoad method which only is run once 
// in this program DisclosureDetail view needs to be loaded for each detail view with different content each time 
- (void)viewWillAppear:(BOOL)animated { 

    NSLog(@"%@", [self.office boxAddress]); 

    [name setText:[self.office name]]; 
    [street setText:[self.office streetAddress]]; 
    if ([[self.office boxAddress] length] > 0) { 
     [box setText:[self.office boxAddress]]; 
    } 
    [zip setText:[NSString stringWithFormat:@"%@ %@", [self.office zipCode], [self.office city]]]; 
    [phone setText:[self.office phoneNo]]; 
    [fax setText:[self.office faxNo]]; 

    [super viewWillAppear:animated]; 
} 

回答

0
if ([[self.office boxAddress] length] > 0) { 
    [box setText:[self.office boxAddress]]; 
} 

如果boxAddress的長度爲0,那麼box將繼續包含任何文本你在它設定的最後一次視圖中顯示。

每次出現視圖時,您都需要隱藏並顯示box,而不僅僅是當視圖加載時,因爲視圖可能已加載,然後用於顯示多個不同的office s。

+0

但我認爲viewWillAppear方法每次都顯示視圖時運行,因此如果'boxAddress'爲空,它不應該顯示?我將如何去隱藏和顯示'box'?如果我隱藏它,它下面的UILabel會被放置在它「可以說」的位置嗎? – 2011-06-15 14:51:26

+0

'viewWillAppear'在你期望的時候被調用,但是沒有什麼會爲你重新設置你的視圖的內容,所以如果你不更新'box',它會繼續顯示上次視圖出現時包含的內容。如果你隱藏'box',其他標籤都不會發生,你需要自己重新定位它們。這種接口的一種常見解決方案是在表格視圖中將每個字段設置爲一個單元格,以便您可以調整顯示哪些單元格並且它們將垂直堆疊。 – Jonah 2011-06-15 14:54:44

+0

所以最簡單的解決方案是使用tableview?一些僞代碼會尋找「cellForRowAtIndexPath」?它會在「行」上使用開關,然後根據行顯示不同的數據? – 2011-06-15 15:20:54

相關問題