2014-04-22 5 views
0

다른 클래스 (DetailView.m에 있음)에서 만든 객체 클래스의 몇 가지 속성을 표시하려고하지만 정확하게 수행하는 방법을 고민하고 있습니다. 나는 그것을 찾으려고 노력했지만 아직 해결책을 찾지 못했다. (나는 objective-c와 iOS dev에 매우 익숙하다.) 기본적인 마스터 뷰 애플리케이션으로 시작했습니다.다른 클래스에 객체 속성 표시

4 명의 플레이어를 표시하는 테이블 뷰를 성공적으로 만들었지 만 사용자가 특정 플레이어를 클릭하면 세 개의 버튼에서 클릭하여 레이블을 가질 수있는 DetailView로 이동합니다. 추가 플레이어 정보가 표시됩니다.

Player.h는 :

#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 

그리고 나는 경우 중 하나를 플레이어의 위치, 나이, 클럽 이름을 표시하는 것을 시도하고있다 :

#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에 어떤 임의의 값을 작성 각각의 버튼이 클릭된다. 나는 이것을 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에서 나는 선언 :

infoLabel.text = [NSString stringWithFormat: @"Plays in the position of @%", self.position] 

을하지만이 작동하지 않습니다 나는() 시도하고 레이블을 변경 positionInfoButton에서 다음을 수행하려고

IBOutlet UILabel *infoLabel; 

. 나는 여기서 자기를 부를 수 있다고 생각하지 않지만 왜 그런지 모르겠습니다. 특정 플레이어의 플레이어 속성을 얻는 또 다른 방법이 있습니까? 내가 제대로 설정을 이해한다면

답변

0

,이 같은 detailItem 재산에 접근해야한다 : 당신의 형식 문자열에 당신이 자리로 @%을 사용하는 것이

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

주 -이 잘못, 문자를 교환 정확한 자리 표시자를 얻으려면 : %@. 당신은 SEGUE,이 같은 준비를 사용해야 할 겁니다

+0

이렇게하면 두 개의 플래그가 표시됩니다. 1은 경고입니다 : "형식은 'id'유형을 지정하지만 인수에는 'CGPoint'유형이 있습니다.".... 또한 일치하지 않는 결과, 매개 변수 또는 유형 속성이있는 '위치'라는 여러 메소드가 있습니다. " 경고를 어떻게 다룰 지 모릅니다. 그리고 나는 내가 두 번 지명 된 자세를 가지고 있지 않다라고 꽤 확신한다. 그래서 나는 조금 길을 잃는다. – mufc

0

,

- (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][email protected]"position"]; 

뭔가 같은 것을해야합니까 직전 , 당신의 details.h 파일에 NSString * 변수가 있는지 확인하십시오.

0

솔루션은 실제로 다음과 같습니다.

상세보기 컨트롤러.HI 다음과 같은 속성을 추가했다 :

@property (strong, nonatomic) Player *detailPlayer; 

postionInfoButton 방법을이 : 마지막으로 다음

- (IBAction)positionInfoButton { 

    infoLabel.text = [NSString stringWithFormat:@"%@", [self.detailPlayer position]]; 
    NSLog(@"Pressed Position button %@", [self.detailPlayer position]); 

} 

그리고, 다음과 같이 prepareForSegue() 메소드를 변경 MasterViewController.m 내 :

if ([[segue identifier] isEqualToString:@"showDetail"]) { 
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
    Player *play = _players[indexPath.row]; 
    [[segue destinationViewController] setDetailPlayer:play]; 
} 

여기서 Player 유형의 인스턴스를 만든 다음 segue를 호출하여 다음보기에 해당 플레이어 정보를 사용하도록 변경했습니다.

위의 제안에 감사드립니다.