0

I have a Master-Detail app and I want to load a new view on the click of rows in Master. If I select row-1, the view is different, and on selection of a different row, new view is presented in Detail portion. How can I achieve this thing?

I am doing something like this. The issue is - If I select any of these 2 rows for the first time, the view actually changes. But again when I click on the other row, it does not change.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

if(indexPath.row==0){


    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 

    TwitterTweetViewController *detailView=(TwitterTweetViewController *)[storyboard instantiateViewControllerWithIdentifier:@"TwitterDetailView"];

    self.splitViewController.mainViewController = detailView;
    [self.splitViewController.mainViewController viewDidLoad];
    [self.splitViewController viewDidLoad];

}


if(indexPath.row == 1)
{

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 

    TwitterDetailViewController *detailView=(TwitterDetailViewController *)[storyboard instantiateViewControllerWithIdentifier:@"TwitterDetailView"];

    self.splitViewController.mainViewController = detailView;
    [self.splitViewController.mainViewController viewDidLoad];
    [self.splitViewController viewDidLoad];

}

}

What should I do in order to change the view on selecting these rows?

Nihal Sharma
  • 2,397
  • 11
  • 41
  • 57
  • Is this what you're looking for? http://stackoverflow.com/questions/9272244/uisplitviewcontroller-on-ipad-with-storyboards –  May 15 '12 at 02:47

3 Answers3

0

There is a great method:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

Use it. It is called right after the click has been made on a cell. You can put some check in there. Short example:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  if (indexPath.row == 0) {
    SomeViewController *svc = [[SomeViewController alloc]initWithNibName:@"SomeViewController" bundle:nil];
    [self.navigationController pushViewController:svc];
  } else if (indexPath.row > 0 && indexPath.row < 5) {
    SomeOtherViewController *sovc = [[SomeOtherViewController alloc]initWithNibName:@"SomeOtherViewController" bundle:nil];
    [self.navigationController pushViewController:sovc];
  } else {
    AnotherViewController *avc = [[AnotherViewController alloc]initWithNibName:@"AnotherViewController" bundle:nil];
    [self.navigationController pushViewController:avc];
  }
  //some other code if needed
}

Hope it helps

Novarg
  • 7,390
  • 3
  • 38
  • 74
  • Getting this error - 2012-05-14 13:56:45.232 SpltiView With Views[26907:f803] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle (loaded)' with name 'NewViewController'' *** First throw call stack: terminate called throwing an exception – Nihal Sharma May 14 '12 at 08:28
  • and since the master-detail has already defined a navigation controller for the default detail view controller - do you think using the [self.navigationController pushViewController:svc] would work? – Nihal Sharma May 14 '12 at 08:45
  • @NihalSharma sorry, didn't see that it was for iPad – Novarg May 14 '12 at 08:55
0

You can create an enum of your view controllers as :-

enum MyViewControllers
{
viewcontroller1
viewcontroller2
viewcontroller3
}

The constatns in enum would correspond to your viewcontrollers for each indexPAth.

and then in the delegate method of UITAbleVIew didSelectRowAtIndexPath: , use switch case :-

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath 
    {
    switch(indexPath.row)
{
    case 1:
          Load view1
          break;
    case 2:
          Load view2
          break;
    case 3:
          Load view3
          break;     .
         .
         so on.
    }
}

Or in case of different NavigationControllers (as in iPad )you will have to pass data using delegates and protocol.Define the protocol in rootViewController and then set its delegate in the detail(Where you want to push the viewcontroller)

somethinglike :-

@protocol SendSelectedINdexDelegate
{
@required
-(void)sendTheIndex:(NSNumber)number.
@end

in interface

id <SendSelectedINdexDelegate> delegateSend

set its property:-

@property (nonatomic) id delegateSend;

in .m

@synthesize delegateSend

and in didSelectRowAtIndexPath:-

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
[self delegate]sendTheIndex:indexPath.row];

}

At the receiving controller conform to protocol and set

rootview.delegateSend=self;

and implement the method of protocol;

-(void)sendTheIndex:(NSNumber)number
{
//nsnumber is your index perform operation based on this.
}
Abhishek Singh
  • 6,068
  • 1
  • 23
  • 25
  • 1
    I do not think it will work. Please suggest something other than this. – Nihal Sharma May 14 '12 at 09:04
  • [self delegate]sendTheIndex:indexPath.row]; should read: [[self delegateSend]sendTheIndex:indexPath.row]; but then you´ll have an incompatible integer to pointer conversion sending NSInteger to a NSNumber parameter. – LAOMUSIC ARTS Jan 31 '14 at 10:01
0

Take care that if you are using ARC,it forbids synth of a property with unspecified ownership attribute( in .m @synthesize delegateSend).

LAOMUSIC ARTS
  • 642
  • 2
  • 10
  • 15