Plz code Help 谁能告诉我如何完成这项任务?在主屏幕中,用户选择足球运动员,在表格视图单元格的第二个屏幕中,用户选择特定行并保存该行并返回主视图。在主视图中,它会显示特定行的视频。基本上我想知道特定的行选择,将该选择保存在表格视图中并在主屏幕中显示他们的内容。
4 回答
通过下面的代码,它实现了委托概念,也实现了你的问题的解决方案希望这对你有帮助:)
//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
简单的解决方案... 由于您是新手,我正在澄清每一点。
首先在 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 } }
实现此目的的好方法:
- 自定义委托
NSNotificationCenter
NSUserDefaults
(编辑:不必要的磁盘写入)- 维护一个 Common NSObject 子类并刷新数据
-willAppear
其他方法:
- 数据库(核心数据/SQLite)或 plist(对于您的情况来说太重了)(编辑:不必要的磁盘写入)
UIPasteBoard
快速委托教程:
第 1 部分:创建委托
假设这是在UITableViewController
我命名的子类的 .h 中YourTableViewControllerClassName
//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
假设这是-didSelectRowAtIndexPath
相应UITableViewController
子类的:
-(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
一个类中获取并设置它,那么您可以在适当的位置进行更改(主要是 中的方法/s )并传递所选项目的数组索引或任何对象/变量/其他让你的生活更轻松UILabel
@protocol
*如果你想要一些更简单的东西,那就去NSNotificationCenter
或者NSUserDefaults
甚至可能UIPasteBoard
(如果它漂浮你的船)
选择任何行时使用调用的 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)使用委托将值返回给前一个控制器
// 尝试 google 并首先了解这个概念,如果你想使用 delgate,请告诉我