Передача данных UILabel между ViewControllers - PullRequest
0 голосов
/ 06 ноября 2011

В моем приложении у меня есть контроллер представления, который содержит две метки и UIDatePicker.Когда я изменяю значение этого указателя даты, метка совпадает с теми же данными.

Рядом со второй меткой у меня есть кнопка, которая переходит к другому контроллеру вида с помощью вертикального перехода.Во втором представлении у меня есть другая метка и UIDatePicker.Когда я изменяю значение UIDatePicker, оно меняет значение метки в том же контроллере представления на (пример) «6 ноября 2011 года».Есть кнопка «Готово», которая отклоняет контроллер представления с помощью «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
...