Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone幻灯片视图传递变量_Iphone_Objective C - Fatal编程技术网

iPhone幻灯片视图传递变量

iPhone幻灯片视图传递变量,iphone,objective-c,Iphone,Objective C,好的,我正在尝试制作一个应用程序,它有一个包含秒表的计算。单击“计算”视图上的按钮时,秒表从底部滑入。这一切都很好,我无法解决的问题是如何将记录的时间发送回前一个控制器以更新文本字段 我简化了代码,去掉了最不相关的东西 非常感谢 CalculationViewController.h #import <UIKit/UIKit.h> @interface CalculationViewController : UIViewController <UITableViewDeleg

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

我简化了代码,去掉了最不相关的东西

非常感谢

CalculationViewController.h

#import <UIKit/UIKit.h>

@interface CalculationViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

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

- (IBAction)showTimer:(id)sender;
@end
#import <UIKit/UIKit.h>

@interface TimerViewController : UIViewController {

IBOutlet UILabel *time;
NSTimer *myTicker;
}

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

#import <UIKit/UIKit.h>

@interface CalculationViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

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

- (IBAction)showTimer:(id)sender;
@end
#import <UIKit/UIKit.h>

@interface TimerViewController : UIViewController {

IBOutlet UILabel *time;
NSTimer *myTicker;
}

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

有几种方法可以做到这一点:

  • 使用NSNotificationCenter,或
  • 使CalculationViewController成为TimerViewController的代理(定义CalculationViewController实现的协议)
在这两种情况下,TimerViewController将通知CalculationViewController它已完成,同时传递数据。因此,在stop方法中,它将执行postNotificationName:object:userInfo或调用委托方法。然后,当CalculationViewController收到通知时,它将在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
#import "TimerViewController.h"
@interface CalculationViewController : UIViewController 
  <UITableViewDelegate, UITableViewDataSource, TimerViewDelegate > {
    ...
}
@end
CalculationViewController.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
#import "TimerViewController.h"
@interface CalculationViewController : UIViewController 
  <UITableViewDelegate, UITableViewDataSource, TimerViewDelegate > {
    ...
}
@end
以下是通知版本:

CalculationViewController.m:

@implementation TimerViewController
@synthesize delegate;
- (IBAction)stop {
    [myTicker invalidate];
    NSString *timerData = @"timer data here";
    [self.delegate timerStopped:timerData];
}
@end
- (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];  
}
- (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];
}
- (IBAction)stop {
    [myTicker invalidate];
    NSDictionary *dict = [NSDictionary dictionaryWithObject:@"timer data here" forKey:@"timerData"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"timerStopped" object:self userInfo:dict];
}
TimerViewController.m:

@implementation TimerViewController
@synthesize delegate;
- (IBAction)stop {
    [myTicker invalidate];
    NSString *timerData = @"timer data here";
    [self.delegate timerStopped:timerData];
}
@end
- (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];  
}
- (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];
}
- (IBAction)stop {
    [myTicker invalidate];
    NSDictionary *dict = [NSDictionary dictionaryWithObject:@"timer data here" forKey:@"timerData"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"timerStopped" object:self userInfo:dict];
}

在这个非常简单的例子中,通知看起来可以使用。无论哪种情况,控制器都不必知道彼此的内部细节,也不必依赖于第三方,如应用程序代理。他们只需就通知名称和用户信息密钥达成一致。协议方法更为严格,但需要自我记录。

哪种方法“正确”呢?我使用主应用程序委托使其工作,但我更希望使计算视图控制器成为计时器视图控制器的委托。您将使用什么代码来执行此操作?谢谢你的帮助。我会很快添加详细信息来回答。非常感谢,我会在早上试一试。