2010-05-09 53 views
0

對,我試圖做一個應用程序,其計算涉及秒錶。點擊計算視圖上的按鈕時,秒錶將從底部滑入。這一切都正常,我無法理解的問題是如何將記錄的時間發送回前一個控制器來更新文本字段。iPhone幻燈片查看傳遞變量

我已經簡化了代碼並刪除了大部分不相關的東西。

非常感謝。

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 

回答

2

一對夫婦的方式做到這一點是:

  • 使用NSNotificationCenter,或
  • 使CalculationViewController TimerViewController的代表(定義CalculationViewController實現協議)

在這兩種情況下, TimerViewController會通知CalculationViewController它已完成並同時傳遞數據。所以在停止方法中,它會執行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。L:

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

在這個非常簡單的情況下,通知的外觀好的使用。在任何一種情況下,控制器都不必知道彼此的內部細節,也不需要像應用程序代理那樣依賴第三方。他們只需要就通知名稱和用戶信息鍵達成一致。協議方法更嚴格,但自我記錄。

+0

這是做「正確」的方式嗎?我使用主應用程序委託來處理它,但最好是讓計算視圖控制器成爲定時器視圖控制器的代表。你會用什麼代碼來做到這一點?謝謝你的幫助。 – sebastyuiop 2010-05-09 14:29:40

+0

將添加詳細信息以儘快解答。 – DyingCactus 2010-05-09 17:29:18

+0

非常感謝,我會在早上給它一個去。 – sebastyuiop 2010-05-09 23:41:00