iPhone幻灯片视图传递变量

时间:2010-05-09 12:45:30

标签: iphone objective-c

是的,我正在尝试制作一个包含秒表计算的应用。单击计算视图上的按钮时,秒表会从底部滑入。这一切都很好,我无法理解的问题是如何将记录的时间发送回前一个控制器来更新文本字段。

我简化了代码并删除了大多数不相关的内容。

非常感谢。

CalculationViewController.h

#import <UIKit/UIKit.h>

@interface CalculationViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

IBOutlet UITextField *inputTxt;
}
@property (nonatomic, retain) UITextField *inputTxt;

- (IBAction)showTimer:(id)sender;
@end

CalculationViewController.m

#import "CalculationViewController.h"
#import "TimerViewController.h"

@implementation CalculationViewController

- (IBAction)showTimer:(id)sender {

   TimerViewController *timerView = [[TimerViewController alloc] init];
   [self.navigationController presentModalViewController:timerView animated:YES];
}

TimerViewController.h

#import <UIKit/UIKit.h>

@interface TimerViewController : UIViewController {

IBOutlet UILabel *time;
NSTimer *myTicker;
}

- (IBAction)start;
- (IBAction)stop;
- (IBAction)reset;
- (void)showActivity;
@end

TimerViewController.m

#import "TimerViewController.h"
#import "CalculationViewController.h"

@implementation TimerViewController

- (IBAction)start { 
myTicker = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(showActivity) userInfo:nil repeats:YES];
}

- (IBAction)stop {
[myTicker invalidate];

#Update inputTxt on calculation view here

[self dismissModalViewControllerAnimated:YES];
}

- (IBAction)reset {
time.text = @"0";
}

- (void)showActivity {
int currentTime = [time.text intValue];
int newTime = currentTime + 1;

time.text = [NSString stringWithFormat:@"%d", newTime];
}
@end

1 个答案:

答案 0 :(得分:2)

有两种方法可以做到:

  • 使用NSNotificationCenter或
  • 使CalculationViewController成为TimerViewController的委托(定义CalculationViewController实现的协议)

在这两种情况下,TimerViewController都会通知CalculationViewController它已完成并同时传递数据。所以在stop方法中,它会执行postNotificationName:object:userInfo或调用委托方法。然后,当它收到通知时,将在CalculationViewController中完成解除。

修改
每次都不一定有正确的方法。根据应用程序的大小和复杂程度以及对情况的确切要求,某些方法比其他方式更好。应用程序委托可以在视图控制器之间共享数据,但最好使用通知或委托来指示控制器之间的事件,就像你想在这里做的那样。

协议是比通知更严格,更自我记录和自包含的方法,但对于这个简单的情况,任何一个都没问题。

以下是使用协议/委托方法实现它的方法:

TimerViewController.h:

@protocol TimerViewDelegate
-(void)timerStopped:(NSString *)timerData;
@end
@interface TimerViewController : UIViewController {
    //other ivars...
    id<TimerViewDelegate> delegate;
}
//other properties...
@property (nonatomic, assign) id <TimerViewDelegate> delegate;
//method declarations...
@end

TimerViewController.m:

@implementation TimerViewController
@synthesize delegate;
- (IBAction)stop {
    [myTicker invalidate];
    NSString *timerData = @"timer data here";
    [self.delegate timerStopped:timerData];
}
@end

CalculationViewController.h:

#import "TimerViewController.h"
@interface CalculationViewController : UIViewController 
  <UITableViewDelegate, UITableViewDataSource, TimerViewDelegate > {
    ...
}
@end

CalculationViewController.m:

- (IBAction)showTimer:(id)sender {
    TimerViewController *timerView = [[TimerViewController alloc] init];
    timerView.delegate = self;
    [self.navigationController presentModalViewController:timerView animated:YES];
    [timerView release];
}

- (void)timerStopped:(NSString *)timerData
{
    inputTxt.text = timerData;
    [self dismissModalViewControllerAnimated:YES];  
}

这是通知版本:

CalculationViewController.m:

- (IBAction)showTimer:(id)sender {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(timerStopped:) name:@"timerStopped" object:nil];
    TimerViewController *timerView = [[TimerViewController alloc] init];
    [self.navigationController presentModalViewController:timerView animated:YES];
    [timerView release];
}
- (void)timerStopped:(NSNotification*)notification
{
    NSString *timerData = [[notification userInfo] objectForKey:@"timerData"];
    inputTxt.text = timerData;
    [self dismissModalViewControllerAnimated:YES];
}

TimerViewController.m:

- (IBAction)stop {
    [myTicker invalidate];
    NSDictionary *dict = [NSDictionary dictionaryWithObject:@"timer data here" forKey:@"timerData"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"timerStopped" object:self userInfo:dict];
}

在这个非常简单的情况下,通知看起来可以使用。在任何一种情况下,两个控制器都不必知道彼此的内部细节,也不依赖于像app代理这样的第三方。他们只需要就通知名称和userinfo密钥达成一致。协议方法更严格,但是自我记录。