如何从另一个视图控制器更新UIButton标签

时间:2010-11-16 14:14:30

标签: iphone objective-c views delegation

我有一个UITabBar应用程序,它有三个选项卡。第一个选项卡有一个带有UIButton的UIViewController,它显示一个模态UIViewController以允许用户选择日期。当用户选择了他们选择的日期时,我无法更新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...

因此,当我单击Save按钮时,它运行 - (void)pickDate:all ok并取消模态视图,但它不会将SearchViewController的UIButton标签更新为“HELLO-TEST”。我确定我在这里错过了一些简单的东西......

请帮助我!!

谢谢:)

3 个答案:

答案 0 :(得分:3)

在主视图控制器中,您需要将“自我”引用传递给搜索日期选择器视图控制器,然后在搜索日期选择器视图中为主视图控制器设置ivar,并在pickDate中使用该引用搜索日期选择器视图的方法。请查看我去年关于这个确切主题的博客文章:

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

答案 1 :(得分:2)

如果您知道如何实现自定义委托方法,则非常容易。只需在选择器视图中使用您想要的参数(它假定在您获取值的位置委托)定义委托方法,然后在UIViewController中定义调用此方法并从委托消息中分配值。如果你想要一些例子,我可以寄给你一些。

答案 2 :(得分:0)