开发者

How to update a UIButton label from another view controller

I have a UITabBar application, which has three tabs. The first tab has a UIViewController with a UIButton which displays a modal UIViewController to allow the user to select a date. I'm having trouble updating the label of the UIButton when the user has selected their chosen date.

My Main View Controller .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

Then my modal window (with a UIPicker, and a couple of UIBarButtons to cancel or save the user's choice) .h / .m looks like:

#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];
开发者_如何学JAVA}

- (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...

So, when I click the Save button, it runs -(void)pickDate: all ok and dismisses the modal view, but it won't update the SearchViewController's UIButton label to "HELLO-TEST". I'm sure I'm missing something simple here...

Please help me!!

Thanks :)


In your main view controller, you need to pass a "self" reference to the search date picker view controller, and then set up an ivar for the main view controller in the search date picker view and use that reference in the pickDate method of the search date picker view. Please check out a blog entry of mine from last year on this exact subject:

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


If you know how to implement your custom delegate method it is very easy. Just define a delegation method with the parameter you want (it suppose to be delegating on the place where you get values) on picker view and then in your UIViewController define calling this method and assigning the value from delegation message. If you want some example I can send you some.


See the below link. Hope it helps someone.

http://www.gisnotes.com/wordpress/2009/08/iphone-note-5-presenting-a-datepicker-as-a-viewcontroller-modally/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜