Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我的代表没有';从表视图切换回根视图时不工作_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 我的代表没有';从表视图切换回根视图时不工作

Ios 我的代表没有';从表视图切换回根视图时不工作,ios,objective-c,uitableview,Ios,Objective C,Uitableview,这是我的citylistTableView控制器,当我按下一个单元格并将所选单元格的文本发送回view控制器的第一个单元格时,我正试图返回ViewController #import <UIKit/UIKit.h> //#import "ViewController.h" @class CityListTableViewController; @protocol CityListTableViewControllerDelegate <NSObject> -(void)

这是我的citylistTableView控制器,当我按下一个单元格并将所选单元格的文本发送回view控制器的第一个单元格时,我正试图返回ViewController

#import <UIKit/UIKit.h>
//#import "ViewController.h"
@class CityListTableViewController;

@protocol CityListTableViewControllerDelegate <NSObject>
-(void)done:(CityListTableViewController*)controllor selectedCity:(UITableViewCell*)citySelected;
@end

@interface CityListTableViewController : UITableViewController{
    NSMutableArray* cities;
}
@property (nonatomic,weak) id<CityListTableViewControllerDelegate> cityDelegate;

@end
下一步:


这就是全部代码,我没有使用segue从一个视图转换到另一个视图。有人能帮我吗?

你需要在这里纠正两件事:

  • 不要将单元格传回,只需将您的城市名称作为NSString传递即可
  • 在rootViewController中,将新城市添加到数据源中
  • 重新加载表格 这将是简单和快速的

    #import "CityListTableViewController.h"
    
    @interface CityListTableViewController ()
    
    @end
    
    @implementation CityListTableViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        // Uncomment the following line to preserve selection between presentations.
        // self.clearsSelectionOnViewWillAppear = NO;
    
        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem;
        cities = [[NSMutableArray alloc] init];
        [self initCities];
    }
    
    -(void)initCities{
        [cities addObject:@"洛杉矶"];
        [cities addObject:@"旧金山"];
        [cities addObject:@"芝加哥"];
        [cities addObject:@"西雅图"];
        [cities addObject:@"华盛顿"];
        [cities addObject:@"纽约"];
        [cities addObject:@"迈阿密"];
        [cities addObject:@"圣地亚哥"];
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    #pragma mark - Table view data source
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    #warning Incomplete implementation, return the number of sections
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    #warning Incomplete implementation, return the number of rows
        return [cities count];
    }
    
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cities" forIndexPath:indexPath];
    
        // Configure the cell...
        cell.textLabel.text = [cities objectAtIndex:indexPath.row];
    
        return cell;
    }
    
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        NSLog(cell.textLabel.text);
        [self.cityDelegate done:self selectedCity:cell];
    
        //go back to root view controllor
        [self.navigationController popToRootViewControllerAnimated:YES];
    
    
    }
    
    
    /*
    // Override to support conditional editing of the table view.
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
        // Return NO if you do not want the specified item to be editable.
        return YES;
    }
    */
    
    /*
    // Override to support editing the table view.
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            // Delete the row from the data source
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        } else if (editingStyle == UITableViewCellEditingStyleInsert) {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }   
    }
    */
    
    /*
    // Override to support rearranging the table view.
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    }
    */
    
    /*
    // Override to support conditional rearranging of the table view.
    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
        // Return NO if you do not want the item to be re-orderable.
        return YES;
    }
    */
    
    /*
    #pragma mark - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */
    
    @end
    
    #import <UIKit/UIKit.h>
    #import "CityListTableViewController.h"
    
    @interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource,UITabBarDelegate,CityListTableViewControllerDelegate>{
    
        IBOutlet UITableView *setTourTableView;
        NSMutableArray *tourSetting;
    
    
    
    }
    
    @end
    
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        // Generate a black tab bar
    
        // Set the selected icons and text tint color
    
        self.view.backgroundColor = [UIColor colorWithRed:0.94 green:0.93 blue:0.93 alpha:1.0];
        setTourTableView.backgroundColor = [UIColor colorWithRed:0.94 green:0.93 blue:0.93 alpha:1.0];
    
    
        tourSetting = [[NSMutableArray alloc] initWithObjects:@"城市", @"日期", @"景点", @"语种",@"人数",nil];
    
        //template for selecting tabbar item
    
    }
    
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
        return [tourSetting count];
    }
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"toursetting"];
    
        cell.textLabel.text = [tourSetting objectAtIndex:indexPath.row];
        return cell;
    }
    
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        NSLog(cell.textLabel.text);
        if([cell.detailTextLabel.text  isEqual: @""]){
            NSLog(@"is null");
        }
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        if(indexPath.row == 0){
            UITableViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"city"];
            [[self navigationController] pushViewController:viewController animated:YES];
        }
        else if(indexPath.row == 1){
            UITableViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"data"];
            [[self navigationController] pushViewController:viewController animated:YES];
        }
        else if(indexPath.row == 2){
            UITableViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"views"];
            [[self navigationController] pushViewController:viewController animated:YES];
        }
        else if(indexPath.row == 3){
            UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"language"];
            [[self navigationController] pushViewController:viewController animated:YES];
        }
        else if(indexPath.row == 4){
            UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"party"];
            [[self navigationController] pushViewController:viewController animated:YES];
        }
    
    
    }
    
    -(void)setTabBarItem:(UITabBarItem *)tabBarItem{
    
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    /********below are delegate method*****************/
    -(void)done:(CityListTableViewController *)controllor selectedCity :(UITableViewCell*)citySelected{
    
        NSIndexPath* path = [NSIndexPath indexPathForRow:0 inSection:0];
        [setTourTableView cellForRowAtIndexPath:path].detailTextLabel.text = citySelected.textLabel.text;
        NSLog(@"dddd");
    }
    
    @end