2012-05-14 13 views
6

Trong ứng dụng của tôi, tôi sử dụng uitable để chọn danh mục từ danh sách của tôi. nhiệm vụ của tôi là, khi người dùng nhấp vào hoặc chọn một ô, anh ấy sẽ có thể xem chi tiết ô được chọn trong chế độ xem tiếp theo (xem chi tiết). và khi anh ta chọn mục trong một khung nhìn chi tiết, anh ta sẽ có thể di chuyển trở lại trong khung nhìn bảng và sẽ có thể thấy mục đã chọn trong bộ điều khiển rootivew.Làm cách nào để chuyển dữ liệu từ bộ điều khiển xem chi tiết sang uitableview?

tôi có thể điều hướng chính xác từ chế độ xem bảng đến chế độ xem chi tiết, nhưng tôi không thể hiển thị mục được chọn trong chế độ xem chi tiết tới rootviewcontroller.

hãy giúp tôi giải quyết vấn đề này.

enter image description here hình ảnh một là trang trình quản lý rootview của tôi. Ví dụ: : nếu người dùng chọn @ "make", anh ta có thể xem tất cả danh mục được xếp hạng lại của @ "make" . trong trang tiếp theo (hình ảnh 2).

enter image description here hình ảnh là trang chi tiết của tôi.

và khi người dùng chọn @ "abarth", nó sẽ được hiển thị trong trang điều khiển chế độ xem gốc (là trang một).

Sau đây là mã của tôi của trang điều khiển 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]; 

} 

Trả lời

8

Bạn cần sử dụng các đại biểu tùy chỉnh. Tạo một giao thức trong detailview của bạn và thực hiện nó trong rootview.Pass chuỗi đã chọn làm tham số cho phương thức ủy nhiệm và từ phương thức ủy nhiệm, hiển thị nó trong trường văn bản của bạn.

//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

Cảm ơn nó hoạt động tốt ngay bây giờ. –

1

Hi bạn sẽ phải làm điều đó bằng giao thức và ủy Xin xem liên kết của tôi on protocol and delegate

bạn cũng có thể làm điều đó bằng cách tạo ra một biến trong appdelegate, thiết lập thuộc tính của nó và truy cập nó trong bất kỳ khung nhìn nào khác.

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

Không làm ứng dụng nàyLàm cách biến tương đối, thực tiễn tệ hại. – hahmed