views:

56

answers:

3

I'm unable to refer to my integer pickedItem declared in my RootViewController.h in my DetailViewController.m file.

Not sure if I should declare this as a global variable, but tried unsuccessfully to do so.

I think this should be simple, but I haven't got any other suggestions (from other posted answers) to work.

Thanks in advance.

+2  A: 

You can access the app delegate through

[[UIApplication sharedApplication] delegate]
this call. And then can access the root view controller's property (assuming you have access to the rootViewController object in appDelagate).

In RootViewController.h

@property(nonatomic, assign) NSInteger myInt

And from anywhere in the code

UIApplicationDelegate *delegate = [[UIApplication sharedApplication] delegate];
delegate.viewController.myInt = 31;

p.s. I have just typed the code, not compiled. So there might be some typo.

taskinoor
+1: You can do this or set up a Singleton. Although many people advocate only putting code in the App Delegate strictly related to handling the application, everyone has their own way of doing things.
iWasRobbed
agreed. app delegate and also root view controller should be light weighted as they r always present in the memory and also have effect on staring the app. singleton is good but designing proper singleton is not that easy. i may come up with unnecessary objects in memory.
taskinoor
A: 

Thanks for the response. However I'm unsure where I'm supposed to put that code. Here's what I have.

This is a split view iPad app which displays sequential pages of a pamphlet. So I have the root controller in its usual place on the left and the pages appear as an image in a UIImageView on the right.

RootView Controller.h

#import <UIKit/UIKit.h>

@class DetailViewController;

@interface RootViewController : UITableViewController {
    DetailViewController *detailViewController;
    //NSInteger pickedItem;

}


@property (nonatomic, retain) IBOutlet DetailViewController *detailViewController;
@property (nonatomic, assign) NSInteger pickedItem;
@end

RootViewController.m

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

    /*
     When a row is selected, set the detail view controller's detail item to 
     the item associated with the selected row.
     */

    detailViewController.detailItem = 
    [NSString stringWithFormat:@"%@", 
     [listOfMovies objectAtIndex:indexPath.row]];

    pickedItem = indexPath.row;

    NSString *intString = [NSString stringWithFormat:@"%d", pickedItem];
    detailViewController.label.text = intString;

}

Once the user selects an item in the table view on the left, I set an integer to tell which page was tapped.

This way, if the user alternatively swipes on the detailviewcontroller (instead of tapping from the table list), the detailviewcontroller will know which page it is currently showing (pickedItem) and will show the next in the sequence via a case statement.

DetailViewController.h

@interface DetailViewController : UIViewController <UIPopoverControllerDelegate, UISplitViewControllerDelegate> {

    UIPopoverController *popoverController;
    UIToolbar *toolbar;
    UITabBar *tabBar;

    id detailItem;
    UILabel *detailDescriptionLabel;
    RootViewController *rootViewController;
    IBOutlet UIImageView *imageView;
    BOOL transitioning;
    BOOL barHidden;
}

@property (nonatomic, retain) IBOutlet UIToolbar *toolbar;

@property (nonatomic, retain) id detailItem;
@property (nonatomic, retain) IBOutlet UILabel *detailDescriptionLabel;
@property (nonatomic, retain) RootViewController *rootViewController;
@property (nonatomic, retain) IBOutlet UILabel *label;
@property (nonatomic, retain) IBOutlet UIView *detailViewController;
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) IBOutlet UIToolbar *toolBar;
@property (nonatomic, retain) IBOutlet UITabBar *tabBar;

@end

DetailViewController.m

//...

@synthesize imageView, toolBar, tabBar, label, rootViewController;

//...        

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
        UITouch *touch = [touches anyObject];
        CGPoint currentPosition = [touch locationInView:self.view];  



    CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x);
    CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y);


    if(deltaX >= kMinimumGestureLength && deltaY <=     kMaximumVariance){
        label.text = @"Horizontal Swipe Detected";
        switch (rootViewController.pickedItem) {
            case 0:
                imageView.image = [UIImage imageNamed:@"Page one.jpg"];
                rootViewController.pickedItem = 1;
                break;
            case 1:
                imageView.image = [UIImage imageNamed:@"Page two.jpg"];
                rootViewController.pickedItem = 2;
                break;
            /...
        }
    }
    else if(deltaY >= kMinimumGestureLength && deltaX <=   kMaximumVariance){
        label.text = @"Vertical Swipe Detected";
    }
}

I have "horizontal swipe detected" statements so that I know it is in fact recording my swipes.

The behavior of the app is that it always treats "itemPicked" as 0, even though the Root View Controller accurately changes the value (I know this from a UILabel it updates).

So, of course, the problem is in the Detail View controller not getting the variable set by Root View Controller.

Thoughts? Thanks.

ajkochanowicz
FYI.. ordinarily you'd hit the "edit" link under your initial question and update that post. Only post answers when it is indeed an answer. This keeps things more organized.
iWasRobbed
A: 

You just wouldn't want it coded this way. I recommend taking a look at NSNotificationCenter, or create a delegate class for yourself, to communicate to the DetailViewController of page changes. There shouldn't be a dependency from detail -> root the way you have it.

Based on your code above, I don't see where you're setting rootViewController on detailViewController? Is it nil? I'd think referencing rootViewController.pickedItem if rootViewController was nil would cause a crash, but worth checking.

Also, is pickedItem being set appropriately? In other words, is it set before the detail code is being called?

ZaBlanc