如何通过数据从细节视图控制器回的UITableView?(How to pass data back

2019-07-29 09:14发布

在我的应用程序我利用uitable的从我的列表中选择类别。 我的任务就是,当过用户点击或选择单元格,他应该能够查看下一个视图(详细视图)选定单元格的细节。 当他在详细视图中选择项目,他应该能够回迁表格视图,应该能够看到一个rootivew控制器所选择的项目。

我能够从实现代码如下详细视图正常浏览,但我不能够显示其详细视图中选择到RootViewController的项目。

请帮我出这个问题。

像一个是我rootview控制器页面。 例如:如果用户选择@“作”,他将能够看到的所有相关型号类别@“作”。 在接下来的页面(其中图像2)。

图片是我的详细信息页面。

并且当用户选择@“阿巴特”在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];

}

Answer 1:

您需要使用自定义的代表。 创建一个在您的DetailView的协议并在rootview.Pass实现它所选字符串作为参数委派方法和从委托方法,在文本字段中显示它。

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


Answer 2:

嗨,你将不得不使用协议,这样做,并委托请参阅我的链接上的协议和委托

你也可以通过创建的appdelegate一个变量,并设置其属性,在任何其他视图访问它做到这一点。

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


文章来源: How to pass data back from detail view controller to uitableview?