2012-05-14 27 views
6

在我的應用程序中,我利用可用來從我的列表中選擇類別。 我的任務是,當用戶點擊或選擇一個單元格時,他應該能夠在下一個視圖(詳細視圖)中查看選定單元格的詳細信息。並且當他在詳細視圖中選擇該項目時,他應該能夠移回到表視圖中,並且應該能夠在rootivew控制器中看到所選項目。如何將數據從詳細視圖控制器傳遞迴uitableview?

我能夠從tableview正確導航到詳細信息視圖,但我無法將詳細視圖中選中的項目顯示給rootviewcontroller。

請幫我解決這個問題。

enter image description here image one is my rootview controller page。例如: :如果用戶選擇@「make」,他將能夠看到@「make」 的所有相關類別。在下一頁(圖2)中。

enter image description here 圖像是我的詳細信息頁面。

當用戶選擇@「abarth」時,它應該顯示在rootview控制器頁面(這是第一頁)。

以下是我rootview控制頁面的代碼:

- (void)viewDidLoad 
{ 

    self.car = [[NSArray alloc]initWithObjects:@"Make",@"Model",@"Price Min",@"Price Max",@"State",nil]; 


    [super viewDidLoad]; 

} 

-(NSInteger) numberOfSectionInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.car count]; 
} 



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *TextCellIdentifier = @"Cell"; 

    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:TextCellIdentifier]; 
    if (cell==nil) 
    { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TextCellIdentifier]; 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 

     cell.textLabel.text = [self.car objectAtIndex:[indexPath row]]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 





     return cell; 
} 



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





     if (0 == indexPath.row) 
    { 
     NSLog(@"0"); 
     self.detailcontroller.title = @"Make"; 
    } 
    else if (1 == indexPath.row) 
    { 
     NSLog(@"1"); 
     self.detailcontroller.title = @"Model"; 
    } 
    else if (2 == indexPath.row) 
    { 
     NSLog(@"2"); 
     self.detailcontroller.title = @"Price Min"; 
    } 
    else if (3 == indexPath.row) 
    { 
     self.detailcontroller.title = @"Price Max"; 
    } 
    else if (4 == indexPath.row) 
    { 
     NSLog(@"3"); 
     self.detailcontroller.title = @"State"; 
    } 
    [self.navigationController 
    pushViewController:self.detailcontroller 
    animated:YES]; 
} 



following is my detail view page code: 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    if ([self.title isEqualToString:@"Make"]) 
    { 
     detail = [[NSArray alloc]initWithObjects:@"Any Make",@"Abarth",@"AC",@"ADAYER",@"Adelaide",@"ALFA ROMEO",@"ALLARD",@"ALPINE-RENAULT",@"ALVIS",@"ARMSTRONG", 
        @"ASTON MARTIN",@"AUDI",@"AUSTIN",@"AUSTIN HEALEY",@"Barossa",@"BEDFORD", 
        @"BENTLEY",@"BERTONE",@"BMW",@"BOLWELL",@"BRISTOL",@"BUICK",@"BULLET", 
        @"CADILLAC",@"CATERHAM",@"CHERY",@"CHEVROLET",@"CHRYSLER",@"CITROEN", 
        @"Country Central",@"CSV",@"CUSTOM",@"DAEWOO",@"DAIHATSU",@"DAIMLER", 
        @"DATSUN",@"DE TOMASO",@"DELOREAN",@"DODGE",@"ELFIN",@"ESSEX", 
        @"EUNOS",@"EXCALIBUR",@"FERRARI",nil]; 

     if ([self.title isEqualToString:@"Abarth"]) 
     { 
      detail = [[NSArray alloc]initWithObjects:@"HI", nil]; 
     } 
    } 
    else if ([self.title isEqualToString:@"Model"]) 
    { 
     detail = [[NSArray alloc]initWithObjects:@"Any Model", nil]; 


    } 
    else if ([self.title isEqualToString:@"Price Min"]) 
    { 
     detail = [[NSArray alloc]initWithObjects:@"Min",@"$2,500", 
        @"$5,000", 
        @"$7,500", 
        @"$10,000", 
        @"$15,000", 
        @"$20,000", 
        @"$25,000", 
        @"$30,000", 
        @"$35,000", 
        @"$40,000", 
        @"$45,000", 
        @"$50,000", 
        @"$60,000", 
        @"$70,000", 
        @"$80,000", 
        @"$90,000", 
        @"$100,000", 
        @"$500,000", 
        @"$1,000,000",nil]; 

    } 
    else if ([self.title isEqualToString:@"Price Max"]) 
    { 
     detail = [[NSArray alloc]initWithObjects:@"Max", 
        @"$2,500", 
        @"$5,000", 
        @"$7,500", 
        @"$10,000", 
        @"$15,000", 
        @"$20,000", 
        @"$25,000", 
        @"$30,000", 
        @"$35,000", 
        @"$40,000", 
        @"$45,000", 
        @"$50,000", 
        @"$60,000", 
        @"$70,000", 
        @"$80,000", 
        @"$90,000", 
        @"$100,000", 
        @"$500,000", 
        @"$1,000,000",nil]; 
    } 
    else if ([self.title isEqualToString:@"State"]) 
    { 
     detail = [[NSArray alloc]initWithObjects:@"Any State", 
        @"Australian Capital Territory", 
        @"New South Wales", 
        @"Northern Territory", 
        @"Queensland",    
        @"South Australia", 
        @"Tasmania", 
        @"Victoria", 
        @"Western Australia",nil]; 
    } 
    [self.tableView reloadData]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    return [detail count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    cell.textLabel.text = [detail objectAtIndex: 
          [indexPath row]]; 


    cell.font = [UIFont systemFontOfSize:14.0]; 
    return cell; 



} 


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


    [self.navigationController popViewControllerAnimated:YES]; 

} 

回答

8

您需要使用自定義代表。在你的detailview中創建一個協議並在你的rootview中實現它。通過選擇的字符串作爲參數來委託方法和委託方法,將它顯示在你的文本框中。

//something like this 
@interface detailViewController 

// protocol declaration 
@protocol myDelegate 
@optional 
    -(void)selectedValueIs:(NSString *)value; 

// set it as the property 
@property (nonatomic, assign) id<myDelegate> selectedValueDelegate; 

// in your implementation class synthesize it and call the delegate method 
@implementation detailViewController 
@synthesize selectedValueDelegate 
// in your didselectRowAtIndex method call this delegate method 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

     [self selectedValueDelegate])selectedValueIs:valueString] ; 
     [self.navigationController popViewControllerAnimated:YES]; 

    } 




@end 

// In your rootViewController conform to this protocol and then set the delegate 

     detailViewCtrlObj.selectedValueDelegate=self; 
//Implement the delegate Method 
    -(void)selectedValueIs:(NSString *)value{ 
     { 
      // do whatever you want with the value string 
     } 
+0

現在感謝它的工作。 –

1

嗨,你要使用它的協議和委託 請參閱我的鏈接on protocol and delegate

你也可以做到這一點通過創建一個變量做在appdelegate中,設置其屬性並以任何其他視圖訪問它。

YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate]; 
delegate.yourVariable; 
+0

不要做這個appDelegate變量的方式,這是非常糟糕的做法。 – hahmed

相關問題