我创建了一个简单的iPhone应用程序,其中包含一个教程(包含5张图像),当用户首次下载该应用程序时,他们可以单击“下一步”并循环浏览这些图像。当前无法再次播放该教程,但是在我的更新中,我想介绍此功能,某些类似的应用程序在其自身的设置中。

我有一个带有三个Tab Bar table view controllersTimelineEventSettings。当用户转到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方法。

任何对此的指导将不胜感激。

最佳答案

只需在设置视图中使用Tutorial调用didSelectRow代码,而忽略hasSeenTutorial代码

TutorialViewController *tutorial = [[TutorialViewController alloc] initWithImages:tutorialImages];放在settingsViewController的didSelectRow方法中,随时显示本教程

10-08 05:57
查看更多