2010-11-16 82 views
1

我有一個UITabBar應用程序,它有三個選項卡。第一個選項卡帶有一個帶有UIButton的UIViewController,它顯示一個模式UIViewController以允許用戶選擇一個日期。當用戶選擇他們選擇的日期時,我無法更新UIButton的標籤。如何從另一個視圖控制器更新UIButton標籤

我的主視圖控制器的.h/.M

#import <UIKit/UIKit.h> 

@interface SearchViewController : UIViewController { 
    IBOutlet UIButton *butFromDate; 
} 

@property (nonatomic, retain) UIButton *butFromDate; 

- (IBAction)pickDate:(id)sender; 

@end 

//////////////////////////////////////////// 

#import "SearchViewController.h" 
#import "SearchDatePickerViewController.h" 

@implementation SearchViewController 

@synthesize butFromDate; 

- (IBAction)pickDate:(id)sender{ 
    SearchDatePickerViewController *sampleView = [[[SearchDatePickerViewController alloc] init] autorelease]; 

    [sampleView setModalTransitionStyle:UIModalTransitionStylePartialCurl]; 
    [self presentModalViewController:sampleView animated:YES]; 
} 

- (void)dealloc { 
    [butFromDate release]; 
    [super dealloc]; 
} 

@end 

然後我的模式窗口(與UIPicker,和一對夫婦UIBarButtons的取消或保存用戶的選擇).H/.M的樣子:

#import <UIKit/UIKit.h> 

@interface SearchDatePickerViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> { 
    NSMutableArray *dates; 
    IBOutlet UIPickerView *picker; 
} 

@property (nonatomic, retain) NSMutableArray *dates; 
@property (nonatomic, retain) UIPickerView *picker; 

- (IBAction)cancelDatePick:(id)sender; 
- (IBAction)pickDate:(id)sender; 

@end 

//////////////////////////////////////////// 

#import "SearchDatePickerViewController.h" 
#import "SearchViewController.h" 
#import "AppDelegate.h" 
#import "Search.h" 

@implementation SearchDatePickerViewController 

@synthesize dates, picker; 

- (IBAction)cancelDatePick:(id)sender{ 
    [self dismissModalViewControllerAnimated:YES]; 
} 

- (IBAction)pickDate:(id)sender{ 
    HolidayCottagesAppDelegate *appDelegate = (HolidayCottagesAppDelegate *)[[UIApplication sharedApplication] delegate]; 
    SearchViewController *searchView = [SearchViewController alloc]; 
    appDelegate.currentSearch.fromDate = [dates objectAtIndex:[picker selectedRowInComponent:0]]; 

    [searchView.butFromDate setTitle:@"HELLO-TEST" forState:UIControlStateNormal]; 
    [self dismissModalViewControllerAnimated:YES]; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSDate* curDate = [NSDate date]; 
    NSCalendar* calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    NSDateComponents* comps = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:curDate]; 
    NSTimeZone* gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 
    dates = [[NSMutableArray alloc] init]; 

    [comps setTimeZone:gmt]; 
    [comps setWeekday:7]; 

    int startCount = comps.week + 1; 
    int endCount = (((52 - startCount) + 52) + startCount); 

    for (startCount; startCount <= endCount; startCount++){ 
     NSDate *tDate = [calendar dateFromComponents:comps]; 
     [dates addObject:tDate]; 
     [comps setWeek:startCount]; 
    } 
} 

// OTHER SHIZZLE HERE BUT NOT REALLY NEEDED TO DISPLAY... 

所以,當我點擊保存按鈕,它運行 - (空)pickDate:一切OK和駁回模式的看法,但它不會更新SearchViewController的UIButton的標籤爲「HELLO-TEST」。我相信我在這裏錯過了簡單的東西...

請幫助我!

謝謝:)

回答

3

在您的主視圖控制器,你需要通過「自我」參考搜索日期選擇器視圖控制器,然後設置爲在搜索日期選取器的主視圖控制器伊娃在搜索日期選擇器視圖的pickDate方法中查看並使用該引用。請看看去年我的博客中對這個確切主題:

http://www.dosomethinghere.com/2009/10/04/passing-values-and-messages-between-views-on-iphone/

+0

英國石油公司,你是一個壁架。我用你的博客文章來解決這個問題。我在輔助視圖控制器中聲明瞭主視圖控制器 - 就像你做的那樣(並修改了我的代碼以反映這一點)。然後,我運行了應用程序,但沒有更新文本(或記錄任何內容),因此我重新檢查了代碼並注意到我錯過了將輔助視圖中聲明的主視圖控制器設置爲'self'的引用。沒有,而鮑勃是我的父親兄弟。乾杯:) – a1phanumeric 2010-11-16 15:31:01

+0

您可以隨時點擊我答案旁邊的小複選標記,將其標記爲接受的答案。 ;) – 2010-11-16 21:53:55

2

如果你知道如何實現自定義的委託方法是很容易的。只需在選擇器視圖中用你想要的參數(它假定是在獲得值的地方進行委派)定義委託方法,然後在你的UIViewController中定義調用此方法並從委派消息中分配值。如果你想要一些例子,我可以給你一些。

相關問題