2014-04-17 2 views
1

두 개의 컨트롤러가 풀어 낼 수있는 세그먼트를 통해 연결되어 있습니다. 첫 번째보기에는 Auction, Make, Model이 3 개의 행으로 표시되는 테이블이 있습니다. 사용자가 "경매"행을 클릭하면 이전 경매의 테이블 뷰로 전송됩니다.되감기 세그먼트를 통한 변수 전달

내가 원하는 것은 경매 (두 번째보기에서 클릭)를 첫 번째보기에 표시하는 것입니다. 풀어주는 segue 작동 있지만 텍스트를 보유 할 고정 변수를 만들려고했을 때 다시 segueing NULL 바뀝니다. 예를 들어, 'label'이라는 staticNSString을 설정했습니다. 두 번째 tableview의 행을 클릭하면 레이블이 해당 셀의 텍스트 값으로 변경됩니다. 그러나 나는 그것을 segue에서 다시 전달할 때 NULL으로 변경됩니다.

#import "searchCar.h" 
#import "auctionTable.h" 

static NSString *label; 

@implementation auctionTable 
@synthesize auctionName; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    //Default section data 
    [email protected][@"All"]; 

    //First section data 
    self.firstItemsArray = @[@"Houston, TX 4/2014",@"Davenport, IA 4/2014",@"Kissimmee, FL 1/2014",@"Las Vegas, NV 1/2014"]; 

//Second section data 
self.secondItemsArray = @[@"Kansas City, MO 12/2013", 
          @"Anaheim, CA 11/2013", 
          @"Davenport, IA 11/2013", 
          @"Schaumburg, IL 10/2013", 
          @"Dallas, TX 9/2013", 
          @"Monterey, CA 8/2013", 
          @"Walworth, WI 8/2013", 
          @"Santa Monica, CA 7/2013", 
          @"Des Moines, IA 7/2013", 
          @"West Allis, WI 7/2013", 
          @"Champaign , IL 6/2013", 
          @"Indianapolis, IN 5/2013", 
          @"Kansas City, MO 4/2013", 
          @"Houston, TX 4/2013", 
          @"Davenport, IA 4/2013", 
          @"Boynton Beach, FL 2/2013", 
          @"Kissimmee, FL 1/2013"]; 

//Third section data 
self.thirdItemsArray = @[@"Kansas City, MO 12/2012", 
          @"Anaheim, CA 11/2012", 
          @"Davenport, IA 11/2012", 
          @"St Charles, IL 10/2012", 
          @"Dallas, TX 9/2012", 
          @"Monterey, CA 8/2012", 
          @"Walworth, WI 8/2012", 
          @"Des Moines, IA 7/2012", 
          @"West Allis, WI 7/2012", 
          @"St. Charles, IL 6/2012", 
          @"St. Paul, MN 6/2012", 
          @"North Little Rock, AR 6/2012", 
          @"Indianapolis, IN 5/2012", 
          @"Houston, TX 4/2012", 
          @"Walworth, WI 4/2012", 
          @"Kansas City, MO 3/2012", 
          @"Kissimmee, FL 1/2012"]; 

//Fourth section data 
self.fourthItemsArray = @[@"Kansas City, MO 12/2011", 
          @"Dallas, TX 10/2011", 
          @"Fontana, WI 9/2011", 
          @"St. Charles, IL 9/2011", 
          @"Monterey, CA 8/2011", 
          @"Walworth, WI 8/2011", 
          @"Des Moines, IA 7/2011", 
          @"West Allis, WI 7/2011", 
          @"St. Charles, IL 6/2011", 
          @"St. Paul, MN 6/2011", 
          @"Indianapolis, IN 5/2011", 
          @"Walworth, WI 3/2011", 
          @"Kansas City, MO 3/2011", 
          @"Kissimmee, FL 1/2011"]; 

//Fifth section data 
self.fifthItemsArray = @[@"Kansas City, MO 12/2010", 
          @"Canal Winchester, OH 11/2010", 
          @"Ft. Lauderdale, FL 10/2010", 
          @"Winsted, MN 10/2010", 
          @"St. Charles, IL 9/2010", 
          @"Monterey, CA 8/2010", 
          @"Walworth, WI 8/2010", 
          @"Des Moines, IA 7/2010", 
          @"St. Charles, IL 6/2010", 
          @"St. Paul, MN 6/2010", 
          @"Indianapolis, IN 5/2010", 
          @"Kansas City, MO 4/2010", 
          @"Kissimmee, FL 1/2010"]; 

//Sixth section data 
self.sixthItemsArray = @[@"Kansas City, MO 12/2009", 
          @"Branson, MO 10/2009", 
          @"St. Charles, IL 10/2009", 
          @"Canal Winchester, OH 9/2009", 
          @"Monterey, CA 8/2009", 
          @"Des Moines, IA 7/2009", 
          @"St. Charles, IL 6/2009", 
          @"St. Paul, MN 6/2009", 
          @"Indianapolis, IN 5/2009", 
          @"Kansas City, MO 3/2009", 
          @"Kissimmee, FL 1/2009"]; 

//Seventh section data 
self.seventhItemsArray = @[@"Kansas City, MO 12/2008", 
           @"St. Charles, IL 10/2008", 
           @"Canal Winchester, OH 9/2008", 
           @"Des Moines, IA 7/2008", 
           @"St. Charles, IL 6/2008", 
           @"St. Paul, MN 6/2008", 
           @"Indianapolis, IN 5/2008", 
           @"Kissimmee, FL 1/2008"]; 

} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
return 7; 
} 


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 

if(section == 0) 
    return @"Default"; 
else if(section == 1) 
    return @"2014"; 
else if(section == 2) 
    return @"2013"; 
else if(section == 3) 
    return @"2012"; 
else if(section == 4) 
    return @"2011"; 
else if(section == 5) 
    return @"2010"; 
else if(section == 6) 
    return @"2009"; 
else if(section == 7) 
    return @"2008"; 
else 
    return 0; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section 
{ 
if(section == 0) 
    return [self.defaultItemsArray count]; 
else if(section == 1) 
    return [self.firstItemsArray count]; 
else if(section == 2) 
    return [self.secondItemsArray count]; 
else if(section == 3) 
    return [self.thirdItemsArray count]; 
else if(section == 4) 
    return [self.fourthItemsArray count]; 
else if(section == 5) 
    return [self.fifthItemsArray count]; 
else if(section == 6) 
    return [self.sixthItemsArray count]; 
else if(section == 7) 
    return [self.seventhItemsArray count]; 
else 
    return 0; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath 
{ 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"auctionCell"]; 

if(indexPath.section == 0) 
{ 
    cell.textLabel.text = [self.defaultItemsArray objectAtIndex:indexPath.row]; 
} 
else if(indexPath.section == 1) 
{ 
    cell.textLabel.text = [self.firstItemsArray objectAtIndex:indexPath.row]; 
} 
else if(indexPath.section == 2) 
{ 
    cell.textLabel.text = [self.secondItemsArray objectAtIndex:indexPath.row]; 
} 
else if(indexPath.section == 3) 
{ 
    cell.textLabel.text = [self.thirdItemsArray objectAtIndex:indexPath.row]; 
} 
else if(indexPath.section == 4) 
{ 
    cell.textLabel.text = [self.fourthItemsArray objectAtIndex:indexPath.row]; 
} 
else if(indexPath.section == 5) 
{ 
    cell.textLabel.text = [self.fifthItemsArray objectAtIndex:indexPath.row]; 
} 
else if(indexPath.section == 6) 
{ 
    cell.textLabel.text = [self.sixthItemsArray objectAtIndex:indexPath.row]; 
} 
else if(indexPath.section == 7) 
{ 
    cell.textLabel.text = [self.seventhItemsArray objectAtIndex:indexPath.row]; 
} 
else{ 
    cell.textLabel.text = @"Not Found"; 
} 

return cell; 
} 



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

label = cell.textLabel.text; 
} 

-(void) prepareForSegue: (UIStoryboardSegue *)segue sender: (id)sender { 
if ([segue.identifier isEqualToString:@"completeEntry"]) { 
    self.auctionName = label; 
} 
} 

@end 
+0

당신은 확실히 좋은 질문을합니다 ... – Fattie

답변

1

didSelectRowAtIndexPath 전에 prepareForSegue가 호출되기 때문에 코드가 작동하지 않는 이유가 있습니다. segues를 사용할 때는 didSelectRowAtIndexPath를 전혀 구현하지 않는 것이 좋습니다. prepareForSegue : sender :에서 보낸 사람 인수에서 선택한 셀을 가져올 수 있습니다. 코드는

-(void) prepareForSegue: (UIStoryboardSegue *)segue sender: (UITableViewCell *)sender { 

    if ([segue.identifier isEqualToString:@"completeEntry"]) { 
     _label = sender.textLabel.text; 
     self.auctionName = _label; 
    } 
} 

그것은, 세포를 제공하고, 데이터를하지 표시하지만, 당신은 당신의 데이터 소스를 설정하는 방식이 있음을하게되지 또한 보통 셀에서 데이터를 얻을 수있는 좋은 습관이다, 다음과 같이한다 조금 어렵다. 여러 개의 배열을 갖는 대신 하나의 배열 배열을 사용하여 훨씬 깨끗한 코드를 얻을 수 있습니다 (그렇다면 다른 모든 배열은 필요하지 않습니다).

+0

고마워! 완벽하게 작동합니다. 나는 아직 초보자이지만 코드를 정리할 것입니다. – JerryCrowley

0

왜 그냥

(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    self.auctionName = cell.textLabel.text; 
} 

를 사용하지 마십시오

여기
#import "searchCar.h" 
#import "auctionTable.h" 
@implementation searchCar 
@synthesize auctionLabel; 

- (IBAction)unwindToList:(UIStoryboardSegue *)segue 
{ 
    auctionTable *unwoundSegue = segue.sourceViewController; 
    self.auctionLabel.text = unwoundSegue.auctionName; 
} 

@end 

이 두 번째이다 : 여기

는 처음이다? 그리고 당신은 auctionName에 대한 @property 정의를 표시하지 않습니다 auctionTable

prepareForSegue 제거 있지만

@property (copy,nonatomic) NSString *auctionName 

로 정의되어야한다 사본은 참조보다는 문자열의 사본을 얻을 수 있도록한다 할당 해제 할 수있는 캐릭터 라인

함으로써, 방법은 스타일의 문제로, auctionTableAuctionTable해야하며 searchCarSearchCar해야 - 클래스는 대문자로 시작합니다.

관련 문제