如何以模态方式打开UITableViewController并传递NSMutableArray

时间:2014-03-20 11:01:31

标签: ios nsmutablearray uitableview

我有一个ViewController,我通过textBox输入一个键。无论是否找到相同的密钥,输入的密钥都会从数据库中提取所有可用记录。记录列表形成NSMUtableArray,我传递给TableViewController,用户可以在其中选择一个。然后在初始ViewController上,应选择此值。

两个问题:

  1. 有没有办法以模态和编程方式打开TableViewController(不是使用StoryBoard)?我有一些"如果"那里的语句应该控制TableViewController是否打开。

  2. 我无法将数组从ViewController传递给TableViewController。由于我对问题1没有答案,我正在使用另一个按钮(测试)。 UITableViewController中的NSLog告诉我那里的数组为null。

  3. 的UIViewController

    // Add a Navigation Point
    -(IBAction) addIcaoNavigation:(id)sender {
    
        // Setting up the path to the navigation points database
        pathNav = [[NSBundle mainBundle] pathForResource:@"navdata_nav"
                                                  ofType:@"txt"];
    
        // Text entered in the textfield is assigned as the Navigation Point
        navigationPoint = txtNavIcao.text;
    
        // If the Departure airport is not determined, this will give an error. Determine     Departure first.
        if ([txtDepIcao.text isEqual:@""] || portDeparture == nil) {
            // Pop-up message that the airport was not found
            UIAlertView* portNotFoundMsg;
            portNotFoundMsg = [[UIAlertView alloc] initWithTitle:@"No Departure airport"
                                                         message:@"Select the Departure airport first"
                                                        delegate:self
                                               cancelButtonTitle:@"OK"
                                               otherButtonTitles:nil, nil];
            [portNotFoundMsg show];
        }
    
        else {
    
            // Create an object for the Navigation Points
            IGDNavigation* navObj = [[IGDNavigation alloc] initWithName: navigationPoint navdataPath: pathNav];
    
            // Creating a list of all points with the same code sorted by the distance from departure
            navigationList = [navObj navDataWithPreviousLatitude:depLatitude PreviousLongitude:depLongitude];
    
            NSLog(@"NAVIGATION LIST: %@", navigationList); // This works, array created
    
    
            // Pass navigation list !!!
            IGANavListTableViewController *listObj = [[IGANavListTableViewController alloc]init];
            [listObj obtainNavList:navigationList];
    
            // Open the UITableViewController
            // THIS IS WHERE YOUR HELP OF Q1 IS NEEDED.
    

    的UITableViewController

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        // Pass the list of Navigation Points
        IGAViewController *mainObj = [[IGAViewController alloc] init];
    
        NSLog(@"VIEW DID LOAD: %@", listOfNavPoints); // listOfNavPoints is nul.
    
        // The following would work though
        /*
         listOfNavPoints = [[NSMutableArray alloc] initWithObjects:
         @"AAAA",
         @"BBBB",
         @"CCCCC",
         nil];
         */
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
    
        //UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    
        // Configure the cell...
        if (cell==nil)  {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }
        }
    
        cell.textLabel.text = [listOfNavPoints objectAtIndex:indexPath.row];
    
        return cell;
    }
    
    
    // Created this method just in case, although it should be enough to have a setter. Either do not work.
    -(void) obtainNavList: (NSMutableArray *) navList {
        listOfNavPoints = navList;
    }
    

    更新:

    更改了代码,但仍无效。为什么??? !!!

     - (void)viewDidLoad {
    [super viewDidLoad];
    
    // Pass the list of Navigation Points
    IGAViewController *mainObj = [[IGAViewController alloc] init];
    
    listOfNavPoints = [[NSMutableArray alloc] init];
    listOfNavPoints = mainObj.navigationList;
    
    NSLog(@"VIEW DID LOAD: %@", listOfNavPoints);
    }
    
    
     NAVIGATION LIST: (
         "NDB|GIG|GERING|41.944356|-103.683|341 Khz|639 nm",
         "NDB|GIG|GINGIN|-31.459722|115.865556|372 Khz|8275 nm"
     )
    
     VIEW DID LOAD null.
    

1 个答案:

答案 0 :(得分:0)

行。我通过prepareForSegue方法传递数据并使用performSegueWithIdentifier方法在IBAction中实现segue来解决了这个问题。感谢大家的帮助。