提问人:user2396021 提问时间:11/13/2013 更新时间:11/19/2013 访问量:5025
在“表视图”中保存所选行并返回主屏幕 UIButtons [已关闭]
Save selected row in Table View and get back to main screen UIButtons [closed]
问:
请代码帮助 谁能告诉我如何完成这项任务? 在主屏幕中,用户选择足球运动员,在第二个屏幕的表格视图单元格中,用户选择特定行并保存该行并返回主 view.in 主视图,然后显示特定行的视频。 基本上,我想知道特定的行选择,将该选择保存在表格视图中,并在主屏幕中显示它们的连接。
答:
使用选择任何行时调用的 tableView 委托
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
appDelegate.selectedindex = indexpath.row;
or
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:indexpath.row] forKey:@"SelcetedIndex"];
}
然后,您可以做 3 件事来获取所选索引
1) 为索引路径制作一个应用程序委托变量,以便您可以在此处设置并在其他控制器上获取值
在 appDelegate 文件中添加属性 @property int selectedIndex;
2) 使用 NSUserDefault 设置选定的索引值
// read userDefault value
[[[NSUserDefaults standardUserDefaults] objectForKey:@"SelcetedIndex"] intValue];
3) 使用 delegate 将值返回给上一个控制器
尝试谷歌并首先了解这个概念,并让我知道您是否想与 Delgate 合作
评论
简单的解决方案...由于您是新手,因此我正在澄清每一点。
首先在 AppDelegate.h 中创建一个属性
@property int selectedRow;
将选定的 indexpath.row 保存在第二个屏幕(即表视图屏幕)中,并导入 AppDelegate.h
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { self.appDelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate]; self.appDelegate.selectedRow=indexPath.row; //saving the row }
在主屏幕上的 viewWillAppear()
-(void)viewWillAppear:(BOOL)animated { if(self.appDelegate.selectedRow!=-1)//check wether row is selected or not { //action to show the specific row videos } }
评论
通过下面的代码,它实现了委托概念,也实现了您的问题的解决方案,希望这对您有所帮助:)
//in your main view controller
#import "ViewController.h"
#import "FootBallPlayersViewController.h"
@interface ViewController ()<FootballPlayerDelegate>//confirms to this delegate
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)whenSelectButtonClicked:(id)sender
{
FootBallPlayersViewController *controller = [[FootBallPlayersViewController alloc]initWithNibName:@"FootBallPlayersViewController" bundle:nil];
controller.delegate = self; //u must set to self
[self presentViewController:controller animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)selectedFootBallPlayer:(NSString *)player
{
//implementation of your delegate method
//hear u are getting the football player name and u can continue further hear
NSLog(@"%@",player);
if([player isEqualToString:@"player1"])
{
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setTitle:player forState:UIControlStateNormal];
[aButton addTarget:self action:@selector(whenFirstPlayerButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; //add the target to self for click events
aButton.frame = CGRectMake(50, 50, 200, 55);
[self.view addSubview:aButton];
}
else
{
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setTitle:player forState:UIControlStateNormal];
aButton.frame = CGRectMake(50, 105, 200, 55);
[aButton addTarget:self action:@selector(whenSecondPlayerButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; //same hear
[self.view addSubview:aButton];
}
}
//now define the action methods
- (void)whenFirstPlayerButtonClicked:(UIButton *)sender
{
NSLog(@"player 1 video start");
}
- (void)whenSecondPlayerButtonClicked:(UIButton *)sender
{
NSLog(@"player 2 video start ");
}
@end
在包含 TableView 的视图中,执行如下操作
在 FootBallPlayersViewController.h 中
#import <UIKit/UIKit.h>
@protocol FootballPlayerDelegate <NSObject> //define a protocol named FootballPlayerDelegate
- (void)selectedFootBallPlayer:(NSString *)player;
@end
@interface FootBallPlayersViewController : UIViewController
{
NSArray *players;
NSString *selectedPlayer;
}
@property (retain, nonatomic) IBOutlet UITableView *playerTable;
@property (nonatomic, assign) id<FootballPlayerDelegate>delegate; //create a delegate
@end
在您的文件中FootBallPlayersViewController.m
#import "FootBallPlayersViewController.h"
@interface FootBallPlayersViewController ()<UITableViewDataSource,UITableViewDelegate>
{
}
@end
@implementation FootBallPlayersViewController
@synthesize delegate; //synthesizing the delegate
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
players = [[NSArray alloc]initWithObjects:@"player1",@"player2", nil];
// players = [[NSArray alloc]initWithObjects:@"player1","player2", nil];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc
{
[players release];
[_playerTable release];
[super dealloc];
}
- (IBAction)whenDoneButtonClicked:(id)sender {
//when done button clicked -->
//send a delegate to main controller
if([self.delegate respondsToSelector:@selector(selectedFootBallPlayer:)])//to avoid crash
{
[self.delegate selectedFootBallPlayer:selectedPlayer]; //call the delegate method hear
}
//dismiss the view
[self dismissViewControllerAnimated:YES completion:nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return players.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"cell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.textLabel.text = [players objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//u can manage check mark and all, i am getting the selected player name
selectedPlayer = [players objectAtIndex:indexPath.row];
}
@end
评论
实现此目的的好方法:
- 自定义委托
NSNotificationCenter
NSUserDefaults
(编辑:不必要的磁盘写入)- 维护一个通用的 NSObject 子类并刷新数据
-willAppear
其他方式:
- 数据库(Core Data / SQLite)或plist(对于您的情况来说太重了)(编辑:不必要的磁盘写入)
UIPasteBoard
快速委托教程:
第 1 部分:创建委托
假设它位于我命名为 YourTableViewControllerClassName
的 UITableViewController
子类的 .h 中
//declare the protocol
@class YourTableViewControllerClassName;
@protocol YourTableViewControllerClassNameDelegate <NSObject>
//@required //uncomment to specify required delegate methods as below
//- (void)requiredMethodNotUsedForThisExample;
@optional
- (void)selectedRow: (NSString *)selectedObj;
@end
@interface YourTableViewControllerClassName : UITableViewController
//declare a weak property to store any object
@property (nonatomic, weak) id <YourTableViewControllerClassNameDelegate> delegate;
@end
假设这是相应 UITableViewController
子类的 -didSelectRowAtIndexPath
:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//the following line is the main thing and can be called
//in any method within this class (placed wisely)
if([[self delegate] respondsToSelector:@selector(selectedRow)]) { //avoid crash
[[self delegate] selectedRow:cell.textLabel.text];
}
[self.navigationController popViewControllerAnimated:YES];
}
第 2 部分:实现委托
假设这是前 UIViewController
子类中某处的代码:
//call this method somewhere
-(void)pushMyTableViewController
{
//declare "UILabel lblText;" in the .h of this class
//lblText = [UILabel alloc] init];
//[lblText setFrame: CGRectMake(0,0,100,35)];
//[self.view addSubview:lblText];
YourTableViewControllerClassName *tvcObj = [[YourTableViewControllerClassName alloc] init];
//for the following line, remember to declare
//<YourTableViewControllerClassNameDelegate> in the .h of this class
//hence declaring that this class conforms to the delegate protocol
[tvcObj setDelegate:self];
[self.navigationController pushViewController:tvcObj animated:YES];
}
这将是您可以在以前的 UIViewController
子类中实现的委托方法:
#pragma mark - Optional YourTableViewControllerClassName Delegate Methods
-(void)selectedRow:(NSString *)selectedObj
{
[lblText setText:selectedObj];
}
注意:这不会解决您的特定问题,因为我们只是根据 UITableViewController
子类中的所选行设置标签。
关键是要展示授权是如何工作的。
此外,如果你能得到cell.textLabel.text
,并在前一个类的UILabel
上设置它,那么你可以在适当的位置进行更改(主要是@protocol
内的方法),并传递所选项目的数组索引或任何对象/变量/任何让你的生活更轻松的东西
*如果你想要一些更简单的东西,那就去吧,或者甚至可能(如果它漂浮着你的船)NSNotificationCenter
NSUserDefaults
UIPasteBoard
评论
NSUserDefaults
UIPasteBoard
评论