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

发布于 2024-10-03 03:10:07 字数 3270 浏览 7 评论 0原文

我有一个 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...

因此,当我单击“保存”按钮时,它会运行-(void)pickDate:一切正常并关闭模态视图,但它不会将 SearchViewController 的 UIButton 标签更新为“HELLO-TEST”。我确信我在这里缺少一些简单的东西......

请帮助我!

谢谢 :)

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];
}

- (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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鲜血染红嫁衣 2024-10-10 03:10:07

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

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

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/

っ〆星空下的拥抱 2024-10-10 03:10:07

如果您知道如何实现自定义委托方法,那就非常容易了。只需在选择器视图上使用您想要的参数定义一个委托方法(它假设在您获取值的地方进行委托),然后在您的 UIViewController 中定义调用此方法并从委托消息中分配值。如果您想要一些例子,我可以给您发送一些。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文