返回按钮到 tableView
Back Button to tableView
我有一个卡拉 OK 应用程序。第一个屏幕上是所有歌曲。有类别的菜单按钮。如果 "Classic Music" 按钮被 select 编辑,第一个屏幕的内容会改变。
当我 select 一个单元格时,会出现一个带有后退按钮的新视图。
我的问题是:当我按下后退按钮时,第一个屏幕上全是歌曲,即使之前只有经典音乐,我不想要这个。
这是后退动作的方法:
- (IBAction)back:(id)sender {
ViewController *xyz=[[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"first"];
[self presentViewController:xyz animated:YES completion:^(void)
{
}];
}
这是第一个视图的实现:
-(void)viewDidLoad {
categoryArray =[[NSMutableArray alloc]init];
categoryArray = [allKeys valueForKey:@"category" ];
feeds=[[NSMutableArray alloc]init];
feeds=[allKeys valueForKey:@"feed_api"];
if ([stories count] == 0) {
for (int i=0; i<[feeds count]; i++) {
NSString * path =[feeds objectAtIndex:i];
[self parseXMLFileAtURL:path];
}
.............
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView.tag==0) {
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
// Set up the cell
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
cell.title.text=[[stories objectAtIndex: indexPath.row] objectForKey: @"title"];
NSString * aux=[[stories objectAtIndex: indexPath.row] objectForKey: @"url"];
NSString * videoHTML =[NSString stringWithFormat: @"<iframe type=\"text/html\" width=\"100\" height=\"80\" src=\"%@\" frameborder=\"0\"></iframe>",aux] ;
[cell.WEB loadHTMLString:videoHTML baseURL:nil];
cell.title.numberOfLines=6;
return cell;}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (!tableView.tag==0) {
int row =indexPath.row;
if (row==0) {
self.title1.text=@"All Songs";
[stories removeAllObjects];
[self parse];
}else
{
self.title1.text=[categoryArray objectAtIndex:indexPath.row-1];
[stories removeAllObjects];
NSString * path =[feeds objectAtIndex:indexPath.row-1];
[self parseXMLFileAtURL:path];}
i=0;
[self showMenu:nil];//sender should be settingsButton;
}
- (void)parseXMLFileAtURL:(NSString *)URL
{
//stories = [[NSMutableArray alloc] init];
//you must then convert the path to a proper NSURL or it won't work
NSURL *xmlURL = [NSURL URLWithString:URL];
// here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
// this may be necessary only for the toolchain
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
// Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
[rssParser setDelegate:self];
// Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];
[rssParser parse];
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
NSLog(@"all done!");
NSLog(@"stories array has %d items", [stories count]);
if (self.tableView.tag==0) {
[self.tableView reloadData];
}
}
你每次按回键时都会创建一个新的视图控制器,你应该将你的 mainViewController 嵌入到导航控制器中。这样你就可以向前推动视图控制器并向后弹出它们。
我有一个卡拉 OK 应用程序。第一个屏幕上是所有歌曲。有类别的菜单按钮。如果 "Classic Music" 按钮被 select 编辑,第一个屏幕的内容会改变。
当我 select 一个单元格时,会出现一个带有后退按钮的新视图。
我的问题是:当我按下后退按钮时,第一个屏幕上全是歌曲,即使之前只有经典音乐,我不想要这个。
这是后退动作的方法:
- (IBAction)back:(id)sender {
ViewController *xyz=[[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"first"];
[self presentViewController:xyz animated:YES completion:^(void)
{
}];
}
这是第一个视图的实现:
-(void)viewDidLoad {
categoryArray =[[NSMutableArray alloc]init];
categoryArray = [allKeys valueForKey:@"category" ];
feeds=[[NSMutableArray alloc]init];
feeds=[allKeys valueForKey:@"feed_api"];
if ([stories count] == 0) {
for (int i=0; i<[feeds count]; i++) {
NSString * path =[feeds objectAtIndex:i];
[self parseXMLFileAtURL:path];
}
.............
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView.tag==0) {
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
// Set up the cell
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
cell.title.text=[[stories objectAtIndex: indexPath.row] objectForKey: @"title"];
NSString * aux=[[stories objectAtIndex: indexPath.row] objectForKey: @"url"];
NSString * videoHTML =[NSString stringWithFormat: @"<iframe type=\"text/html\" width=\"100\" height=\"80\" src=\"%@\" frameborder=\"0\"></iframe>",aux] ;
[cell.WEB loadHTMLString:videoHTML baseURL:nil];
cell.title.numberOfLines=6;
return cell;}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (!tableView.tag==0) {
int row =indexPath.row;
if (row==0) {
self.title1.text=@"All Songs";
[stories removeAllObjects];
[self parse];
}else
{
self.title1.text=[categoryArray objectAtIndex:indexPath.row-1];
[stories removeAllObjects];
NSString * path =[feeds objectAtIndex:indexPath.row-1];
[self parseXMLFileAtURL:path];}
i=0;
[self showMenu:nil];//sender should be settingsButton;
}
- (void)parseXMLFileAtURL:(NSString *)URL
{
//stories = [[NSMutableArray alloc] init];
//you must then convert the path to a proper NSURL or it won't work
NSURL *xmlURL = [NSURL URLWithString:URL];
// here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
// this may be necessary only for the toolchain
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
// Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
[rssParser setDelegate:self];
// Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];
[rssParser parse];
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
NSLog(@"all done!");
NSLog(@"stories array has %d items", [stories count]);
if (self.tableView.tag==0) {
[self.tableView reloadData];
}
}
你每次按回键时都会创建一个新的视图控制器,你应该将你的 mainViewController 嵌入到导航控制器中。这样你就可以向前推动视图控制器并向后弹出它们。