我试图显示我在另一个类(在 DetailView.m 中)中创建的对象类的一些属性,但我正在努力弄清楚如何做到这一点。我已经尝试过查找但尚未提出解决方案(我对 Objective-c 和 iOS 开发人员非常陌生)。我从一个基本的主视图应用程序开始。
我成功地拥有了一个显示 4 个不同玩家的 tableview,但现在我想要的是,当用户点击特定玩家时,他们将进入 DetailView,他们可以从中点击三个按钮并有一个标签,该标签将显示添加玩家信息。
播放器.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.hi 中声明:
IBOutlet UILabel *infoLabel;
我尝试在 positionInfoButton() 中执行以下操作以尝试更改标签:
infoLabel.text = [NSString stringWithFormat: @"Plays in the position of @%", self.position]
但这不起作用。我不认为我可以在这里调用 self 但我不知道为什么。是否有另一种方法可以为该特定玩家获取该玩家属性?