0

我试图显示我在另一个类(在 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 但我不知道为什么。是否有另一种方法可以为该特定玩家获取该玩家属性?

4

3 回答 3

0

如果我正确理解您的设置,您应该可以访问该detailItem属性,如下所示:

- (IBAction)positionInfoButton {
    infoLabel.text = [NSString stringWithFormat: @"Plays in the position of %@", [self.detailItem position];
}

请注意,在您用作@%占位符的格式字符串中 - 这是错误的,请交换字符以获得正确的占位符:%@.

于 2014-04-22T23:43:16.493 回答
0

解决方案实际上如下:

在 DetailViewController.HI 中必须添加以下属性:

@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 以将该玩家信息用于以下视图。

感谢您的上述建议。

于 2014-04-23T01:34:03.160 回答
0

你将不得不使用prepare for 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

于 2014-04-23T00:37:38.207 回答