我創建了一個簡單的iPhone應用程序,該應用程序在用戶下載應用程序時有一個教程(一系列5張圖像)第一次他們可以點擊下一步並循環顯示圖像。目前無法再次播放教程,但在我的更新中,我想介紹此功能,有些應用程序在其應用程序本身的設置中。在應用內設置中再次播放iOS應用內教程當用戶選擇「再次播放教程」
我有一個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方法不會被調用。
對此的任何指導將非常感激。
目前的教程只需調用你用'教程代碼didSelectRow'在你的設置視圖中省略了'hasSeenTutorial'元素 – JSA986
Thanks @ JSA986 - 我已經把 - (void)displayTutorial代碼放在應用設置中,從DidSelectRow調用,但它似乎永遠不會被調用。我沒有一個故事板視圖控制器的教程再次或任何東西,沒有從繼續播放教程細胞「繼續」..我錯過了什麼? – amitsbajaj
你說你沒有本教程的storyboardViewController,但在你的代碼中有'TutorialViewController'?你應該放置'TutorialViewController * tutorial = [[TutorialViewController alloc] initWithImages:tutorialImages]; [self presentViewController:tutorial animated:YES completion:nil];'在您的'didSelectRow'設置中展示教程 – JSA986