1

Im using Xcode 4.3.2 with storyboards as i'm a newbie so code knowledge is not very good. I have so far created by main view controller which links to my tableview which contains data. However i dont know how to link this to a new view controller, i.e. if i drag a new view controller onto the storyboard page i want to be able to select the cell in the tableview and then opens a new page which contains the details. my code so far with the help of tutorials is:-

Viewcontroller.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITabBarDelegate, UITableViewDataSource, UISearchBarDelegate>
    {
    IBOutlet UITableView * tableView;
    IBOutlet UISearchBar * searchBar;

        NSArray * allItems;
        NSMutableArray * displayItems;
}

@end

Viewcontroller.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    allItems = [[NSArray alloc] initWithObjects:@"K12", @"K14", @"K16", @"K18", @"K42", @"K43", @"K46", @"K53", @"K60", @"K68", @"K81", @"K83", nil];
    displayItems = [[NSMutableArray alloc] initWithArray:allItems];

}

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

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

- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
    cell.textLabel.text = [displayItems objectAtIndex:indexPath.row];
    return cell;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    if ([searchText length] == 0) {
        [displayItems removeAllObjects];
        [displayItems addObjectsFromArray:allItems];
    } else {
        //here
        [displayItems removeAllObjects];
        for (NSString * string in allItems) {
            NSRange r = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
            if (r.location != NSNotFound) {
                [displayItems addObject:string];
        }
    }
}

    [tableView reloadData];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)aSearchBar {
    [searchBar resignFirstResponder];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

I feel comfortable working out the rest of the app just stuck on this bit and searched tutorials looked at various code but because my knowledge is very limited I just need a dummies guide or better still if someone can tell me what code to add that will be great.

Thanks Guys and help would be appreciated.

Andy
  • 11
  • 1
  • 3
  • Do you want a different viewController to be presented, depending on the content of the selected textLabel, or depending on which row is selected? It looks like the number of rows will be different, and their content different, based on a search, so that will complicate things just a bit. FYI not sure it's possible in Storyboard-- you will most likely have to use nibs. – AMayes Apr 11 '12 at 18:29
  • Also, it depends on whether you actually need to use different viewControllers for different selections, or whether you can use the same viewController, with different data displayed. – AMayes Apr 11 '12 at 18:31
  • Thanks for your response. Yes each selected row would contain different data on the new view so if i selected "K12" then the information for K12 would display its own view/page. For Example if it was labelled up "ford vehicle" then when you click on it, it would display the vehicle details. As for Nibs I'm not sure how to add a nib file in 4.3.2 Xcode. I was kind of hoping to drag a new view controller onto the storyboard then link to it somehow but if i have to use a nib file then any help would be appreciated.. Thanks Again for your time. – Andy Apr 12 '12 at 09:01
  • I could use the same view controller for different data displayed depending on which cell is selected if possible – Andy Apr 12 '12 at 09:05

1 Answers1

0

Okay, I will assume that you will use the same nib to display the information, regardless of which row is selected. First, right-click or ctrl-click on your "Views" folder, and select "New File". Create a new UIViewController class, with .xib. My view controller will have a label (for the name of the car), a UIImageView (of the car, populated by an image that has the same name as the car, with a .jpg appendix), and a textView populated by a dictionary. The textView will display information about the car, and I have pre-loaded the dictionary with that information, keyed to the name of the car, like so:

[dictionary setObject:@"This is a car." forKey:@"Ford Taurus"];

The dictionary is a singleton, so every class can access it (see https://stackoverflow.com/a/10093449/542400 for how to implement that). Now that I have my new viewController (let's call it "NewVC"), with a synthesized string (so my first viewController can access it), I will make sure my first viewController will respond when a row is selected:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NewVC *new = [[NewVC alloc] init];
    //assuming you have no array from which you populated your table
    new.myString = [[[myTable cellForRowAtIndexPath:indexPath] textLabel] text];

    //assuming you have a navigationController
    [self.navigationController pushViewController:new animated:YES]; 
}

Now in my NewVC's -viewWillAppear method, I implement the following:

-(void)viewWillAppear:(BOOL)animated{
    [myLabel setText:myString];
    NSString *string = [NSString stringWithFormat:@"%@.jpg", myString];
    UIImage *image = [UIImage imageNamed:string];
    [myImageView setImage:image];
    //see above for sharedDictionary info
    [myTextView setText: [[MainDictionary sharedDictionary]getStringForKey:myString]];
    [super viewWillAppear:YES];
}

This is a very basic implementation, but I hope it will make the process clear. By instituting a simple naming convention for the dictionary keys and image names, you will be making your job a lot easier.

Community
  • 1
  • 1
AMayes
  • 1,767
  • 1
  • 16
  • 29