在我的应用程序中,我有一个视图控制器,它包含两个标签和一个UIDatePicker。当我更改该datepicker的值时,标签匹配相同的数据。
在第二个标签旁边,我有一个按钮,使用垂直过渡转到另一个视图控制器。在第二个视图中,我有另一个标签和一个UIDatePicker。当我更改UIDatePicker的值时,它会将同一视图控制器中的标签值更改为(示例)“2011年11月6日”。有一个完成按钮,通过“dismiss modalViewController”解除视图控制器。我想知道的是,如何将标签的数据从第二个视图控制器传输到第一个视图控制器中的标签。
我是一个相当新的程序员,所以我感谢所有的帮助。
这是我的代码:
ViewController 1 .h:
#import <UIKit/UIKit.h>
@interface PushedViewController : UIViewController {
IBOutlet UIDatePicker *datePicker;
IBOutlet UILabel *product;
IBOutlet UILabel *howLong1;
}
@property (nonatomic, retain) IBOutlet UIDatePicker *datePicker;
@property (nonatomic, retain) IBOutlet UILabel *product;
@property (nonatomic, retain) IBOutlet UILabel *howLong1;
-(IBAction)choose:(id)sender;
-(IBAction)gotoWarranty:(id)sender;
@end
ViewController 1 .m
#import "PushedViewController.h"
#import "WarrantyNextViewController.h"
@implementation PushedViewController
@synthesize datePicker;
@synthesize product, howLong1;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(IBAction)choose:(id)sender{
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSString *myDate;
myDate = [dateFormatter stringFromDate:[datePicker date]];
product.text = myDate;
//UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Purchased on" message:myDate delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
//[alert show];
//[alert release];
//[myDate release];
}
-(IBAction)gotoWarranty:(id)sender{
WarrantyNextViewController *any = [[WarrantyNextViewController alloc] initWithNibName:@"WarrantyNextViewController" bundle:nil];
any.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:any animated:YES];
[any release];
}
- (void)dealloc
{
[datePicker release];
[product release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
product.text = @"Use Date Picker";
self.title = @"Product";
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
self.datePicker= nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
ViewController 2 .h:
#import <UIKit/UIKit.h>
@class PushedViewController;
@interface WarrantyNextViewController : UIViewController{
IBOutlet UIDatePicker *datePicker2;
IBOutlet UILabel *product2;
}
@property (nonatomic, retain) IBOutlet UIDatePicker *datePicker2;
@property (nonatomic, retain) IBOutlet UILabel *product2;
-(IBAction)Done:(id)sender;
-(IBAction)Choose2:(id)sender;
@end
ViewController 2 .m
#import "WarrantyNextViewController.h"
#import "PushedViewController.h"
@implementation WarrantyNextViewController
@synthesize datePicker2, product2;
-(IBAction)Done:(id)sender{
[self dismissModalViewControllerAnimated:YES];
//NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
//[prefs setObject:label2.text forKey:@"label"];
}
-(IBAction)Choose2:(id)sender{
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSString *myDate;
myDate = [dateFormatter stringFromDate:[datePicker2 date]];
product2.text = myDate;
//UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Purchased on" message:myDate delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
//[alert show];
//[alert release];
//[myDate release];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
product2.text = @"Use Date Picker";
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end