我创建了一个简单的 iPhone 应用程序,当用户第一次下载应用程序时,该应用程序有一个教程(一系列 5 张图像),他们可以单击下一步并循环浏览图像。目前没有办法再次播放教程,但在我的更新中,我想介绍这个功能,一些类似的应用程序在其应用程序本身的设置中。
我有一个Tab Bar
与三个table view controllers
Timeline
,Event
和Settings
。当用户转到Settings
tab
并选择“Play Tutorial Again" Table View Cell,
我希望它再次开始教程。
在我的Timeline
视图(根视图)中,我有以下代码:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if ([TutorialViewController hasSeenTutorial] == NO) {
NSArray *tutorialImages = @[[UIImage imageNamed:@"Tutorial 1.png"],
[UIImage imageNamed:@"Tutorial 2.png"],
[UIImage imageNamed:@"Tutorial 3.png"],
[UIImage imageNamed:@"Tutorial 4.png"],
[UIImage imageNamed:@"Tutorial 5.png"]];
TutorialViewController *tutorial = [[TutorialViewController alloc] initWithImages:tutorialImages];
[self presentViewController:tutorial animated:YES completion:nil];
[TutorialViewController setHasSeenTutorial:YES];
}
}
- (void)displayTutorial
{
NSArray *tutorialImages = @[[UIImage imageNamed:@"Tutorial 1.png"],
[UIImage imageNamed:@"Tutorial 2.png"],
[UIImage imageNamed:@"Tutorial 3.png"],
[UIImage imageNamed:@"Tutorial 4.png"],
[UIImage imageNamed:@"Tutorial 5.png"]];
TutorialViewController *tutorial = [[TutorialViewController alloc] initWithImages:tutorialImages];
[self presentViewController:tutorial animated:YES completion:nil];
}
TutorialViewController 的代码是:
static NSString * const kHasSeenTutorial = @"hasSeenTutorial";
+ (BOOL)hasSeenTutorial
{
return [[NSUserDefaults standardUserDefaults] boolForKey:kHasSeenTutorial];
}
+ (void)setHasSeenTutorial:(BOOL)hasSeenTutorial
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:hasSeenTutorial forKey:kHasSeenTutorial];
[defaults synchronize];
}
- (id)initWithImages:(NSArray *)imageArray
{
self = [super init];
if (self) {
if (imageArray == nil) {
[[NSException exceptionWithName:NSInvalidArgumentException reason:@"imageArray cannot be nil." userInfo:nil] raise];
}
self.images = imageArray;
}
return self;
}
- (void)loadView
{
[super loadView];
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.view addSubview:imageView];
self.imageView = imageView;
CGRect buttonFrame = [[UIScreen mainScreen] bounds];
buttonFrame.origin.y = 519.0;
buttonFrame.origin.x = 250.0;
buttonFrame.size.height = 51.0;
buttonFrame.size.width = 70.0;
UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[nextButton setTitle:@"Next" forState:UIControlStateNormal];
[nextButton addTarget:self action:@selector(nextButtonWasTapped:) forControlEvents:UIControlEventTouchUpInside];
nextButton.frame = buttonFrame;
nextButton.backgroundColor = [UIColor whiteColor];
nextButton.showsTouchWhenHighlighted = YES;
nextButton.adjustsImageWhenHighlighted = NO;
nextButton.tintColor = [UIColor purpleColor];
nextButton.titleLabel.font = [UIFont systemFontOfSize:18.0];
nextButton.opaque = NO;
[self.view addSubview:nextButton];
self.nextButton = nextButton;
// ----------------------------------------------------------------------
currentIndex = 0;
if (self.images == nil) {
self.images = @[];
}
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self resetToStart];
}
- (void)setImages:(NSArray *)images
{
_images = images;
[self resetToStart];
}
- (void)resetToStart
{
[self.nextButton setTitle:@"Next" forState:UIControlStateNormal];
currentIndex = 0;
if (currentIndex < self.images.count) {
self.imageView.image = self.images[currentIndex];
}
[self.view bringSubviewToFront:self.nextButton];
}
- (void)nextButtonWasTapped:(id)sender
{
currentIndex++;
if (currentIndex < self.images.count) {
self.imageView.image = self.images[currentIndex];
if (currentIndex == self.images.count - 1) {
[self.nextButton setTitle:@"Start" forState:UIControlStateNormal];
}
}
if (currentIndex == self.images.count) {
[self dismissViewControllerAnimated:YES completion:nil];
[EnvylopeTutorialViewController setHasSeenTutorial:YES];
}
}
从它的外观来看,我猜我会做一些类似调用 ResetToStart 或类似的事情,但我真的不确定我会如何做到这一点。图像在时间轴中进行通信,但我正在从设置中播放教程,所以我猜我必须在设置表视图等中设置一些类似的方法。我真的迷失了这个
因此,从“设置”选项卡中,我希望能够通过单击“播放教程”表格视图单元格来使用相同的图像、按钮等重新播放教程。
在应用程序设置视图控制器中,我有:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if ([cell.textLabel.text isEqualToString:@"Play Tutorial Again"])
{
NSLog(@"The Tutorial is going to play");
[self displayTutorial];
}
}
但是没有调用 displayTutorial 方法。
对此的任何指导将不胜感激。