ios - 当用户选择 "Play Tutorial Again"时,从应用内设置再次播放 iOS 应用内教程

标签 ios iphone cocoa-touch uiimage walkthrough

我创建了一个简单的 iPhone 应用程序,当用户第一次下载该应用程序时,他们可以单击“下一步”并循环浏览图像。目前没有办法再次播放教程,但在我的更新中,我想介绍这个功能,一些类似的应用程序在应用程序本身的设置中。

我有一个 Tab Bar,其中包含三个 table view controllers TimelineEventSettings。当用户转到 Settings tab 并选择“Play Tutorial Again”表格 View 单元格时, 我希望它再次开始教程。

在我的 Timeline View ( Root View )中,我有以下代码:

- (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 或类似的事情,但我真的不确定我会怎么做。图像是在时间轴中传达的,但我正在从设置中播放教程,所以我猜我必须在设置 TableView 等中设置一些类似的方法。我真的迷失了这一点

因此,在“设置”选项卡中,我希望能够通过单击“播放教程”表格 View 单元格,使用相同的图像、按钮等重新播放教程。

在 App Settings View Controller 中,我有:

- (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 方法。

任何关于这方面的指导将不胜感激。

最佳答案

只需在设置 View 中使用 didSelectRow 调用您的 Tutorial 代码,省略 hasSeenTutorial 代码

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

关于ios - 当用户选择 "Play Tutorial Again"时,从应用内设置再次播放 iOS 应用内教程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22632498/

相关文章:

ios - Webkit 滚动条错误

ios - 是否可以在其他 iOS 应用程序中访问 iOS 游戏应用程序的分数?

iphone - 如何使用带有 NSJSONSerialization 类引用的 xcode 4.3.1 从 URL 获取数据

iphone - 如何完全禁用 UITextView 的滚动?

iphone - 如何使模态视图可滚动?

ios - 在不引用 iOS 7 的情况下查找 UIAlertView

ios - 隐藏 View 的视觉约束

ios - 如何设置可以在 swift 5 中实例化后推送的初始 viewController

iphone - 如何使用 RestKit .20 "Fire and forget"某些请求,例如 DELETE 或 PUT 甚至某些 POST?

ios - swift 4 : Downcast callback function issue