我正在尝试显示我在另一个类(在DetailView.m中)创建的对象类的一些属性,但我正在努力弄清楚到底是怎么做的。我已经尝试过查找但尚未提出解决方案(我对Objective-c和iOS dev非常新)。我从一个基本的主视图应用程序开始。
我成功地拥有一个显示4个不同玩家的桌面视图,但现在我想要的是当用户点击特定玩家时他们将移动到DetailView,他们可以从三个按钮中点击它并且有一个标签将显示添加的玩家信息。
Player.h:
#import <Foundation/Foundation.h>
@interface Player : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *position;
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, strong) NSString *club;
-(NSString *) playerDetail;
@end
我在AppDelegate.m中填写了一些随机值:
#import "AppDelegate.h"
#import "MasterViewController.h"
#import "Player.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
Player *player1 = [[Player alloc]init];
player1.name = @"Cristiano Ronaldo";
player1.age = 27;
player1.position = @"Winger";
player1.club = @"Real Madrid";
Player *player2 = [[Player alloc]init];
player2.name = @"Neymar";
player2.age = 21;
player2.position = @"Striker";
player2.club = @"Barcelona";
Player *player3 = [[Player alloc]init];
player3.name = @"Juan Mata";
player3.age = 25;
player3.position = @"Midfielder";
player3.club = @"Manchester United";
Player *player4 = [[Player alloc]init];
player4.name = @"Thiago Silva";
player4.age = 27;
player4.position = @"Center Back";
player4.club = @"PSC";
NSMutableArray *players = [NSMutableArray arrayWithObjects:player1, player2, player3, player4, nil];
UINavigationController *navController = (UINavigationController *) self.window.rootViewController;
MasterViewController *masterController = [navController.viewControllers objectAtIndex:0];
masterController.players = players;
return YES;
}
//Rest of code is the generic boilerplate provided by Xcode
@end
我试图在点击任意一个按钮时显示玩家的位置,年龄,俱乐部名称。我尝试在DetailViewController.m中处理它。
DetailViewController.m:
#import "DetailViewController.h"
#import "Player.h"
@interface DetailViewController ()
- (void)configureView;
@end
@implementation DetailViewController
#pragma mark - Managing the detail item
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
self.detailDescriptionLabel.text = [self.detailItem description];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)positionInfoButton {
//Stuck here
}
- (IBAction)ageInfoButton {
//Stuck here
}
- (IBAction)clubInfoButton {
//Stuck here
}
我陷入了postionInfoButton等的动作方法,我试图在用户点击detailView中的按钮时显示玩家位置。在DetailView.h中我声明了:
IBOutlet UILabel *infoLabel;
我尝试在positionInfoButton()中执行以下操作以尝试更改标签:
infoLabel.text = [NSString stringWithFormat: @"Plays in the position of @%", self.position]
但这不起作用。我不认为我可以在这里打电话给自己,但我不知道为什么。是否有其他方法可以获得该特定玩家的玩家资产?
答案 0 :(得分:0)
如果我理解您的设置正确,您应该可以访问detailItem
属性,如下所示:
- (IBAction)positionInfoButton {
infoLabel.text = [NSString stringWithFormat: @"Plays in the position of %@", [self.detailItem position];
}
请注意,在格式字符串中,您使用@%
作为占位符 - 这是错误的,交换字符以获取正确的占位符:%@
。
答案 1 :(得分:0)
你将不得不使用准备segue,像这样,
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Assume self.view is the table view
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
DetailObject *detail = [self detailForIndexPath:path];
[segue.destinationViewController setDetail:detail];
}
在你做segue.destinationViewController之前,做这样的事情
[detail setPosition: players[indexPathForSelectedRow]objectForKey@"position"];
有些东西,确保在你的details.h文件中你有一个变量NSString * position
答案 2 :(得分:0)
解决方案实际上如下:
在DetailViewController.H中,我必须添加以下属性:
@property (strong, nonatomic) Player *detailPlayer;
postionInfoButton方法执行此操作:
- (IBAction)positionInfoButton {
infoLabel.text = [NSString stringWithFormat:@"%@", [self.detailPlayer position]];
NSLog(@"Pressed Position button %@", [self.detailPlayer position]);
}
最后,在MasterViewController.m中更改prepareForSegue()方法,如下所示:
if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
Player *play = _players[indexPath.row];
[[segue destinationViewController] setDetailPlayer:play];
}
这里我进行了更改以创建Player类型的实例,然后调用segue将该播放器信息用于以下视图。
感谢您提出上述建议。