views:

389

answers:

1

I'm relatively new, embarking on my second app, and having major pains with multiple view controllers. I've added the relevant bits of code to the bottom of this email. Here's what's happening:

MainViewController: creates RoomViewController, then asks for its view RoomViewController: sets up the room with the items in it (in this case, Hector and a coffee). Room.m: background of the room Item.m: gets all its info, creates itself, enables user interaction. (touchesBegan etc)

The problem is: When item is created in RoomViewController, the touches don't work. When item is created in MainViewController, the touches work (even when created to go within roomView).

Is there some kind of torch i need to hand off to the RoomViewController to tell it to accept interactions? I've been literally banging my head against the desk and throwing things because I'm sure it must be dead simple but I've spent hours trying to pin it down. Any help you can provide would make me extremely happy.

-k.

Main view controller:

- (void)viewDidLoad {
    RoomViewController *viewController = [[RoomViewController alloc] initWithNibName:@"RoomViewController" bundle:nil];
    viewController.mainViewController = self;
    self.roomViewController = viewController;
    [viewController release];
    roomView = self.roomViewController.view;
    [self.view addSubview:self.roomViewController.view];
}

Room view controller:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
     room = [[Room alloc] initRoom:@"OFFICE" displayName:@"Office"];
     [self.view addSubview:room];

     NSString *itemName = @"COFFEE";
     CGPoint iLocation = CGPointMake(82, 192);
     CGPoint hLocation = CGPointMake(120, 200);
     NSString *hFace = @"FL";

     Item *newItem = [[Item alloc] initItem:itemName viewController:self atLocation:iLocation];
     [self.view addSubview:newItem];

     [newItem release];

     [self.view addSubview:hector];
     hector.center = CGPointMake(350, 200);  
    }
    return self;
}

Room.m:

-(id)initRoom:(NSString *)rName displayName:(NSString *)rDisplay {
    roomName = rName;
    displayName = rDisplay;
    NSString *bgFile = [NSString stringWithFormat:@"%@_BG.png", roomName];
    [self initWithImage:[UIImage imageNamed:bgFile]];
    [self setCenter:CGPointMake(240, 135)];
    return self;
}

Item.m:

- (id)initItem:(NSString *)iName viewController:(RoomViewController *)vc atLocation:(CGPoint)iLocation {

    itemName = iName;

    NSString *itemFile = [NSString stringWithFormat:@"%@_ITEM.png", itemName];
    [self initWithImage:[UIImage imageNamed:itemFile]];
    [self setCenter:iLocation];
    myViewController = vc;
    self.userInteractionEnabled = YES;

    return self;
}   

-(void)touchesBegan... etc
+1  A: 

Got it. I feel like a bit of a fool now. It was a two-fold problem.

1) I put clipsToBounds on... turns out my RoomVC wasn't the right size, so the items weren't in the active area. But why wasn't it the right size?

2) In the XIB, my RoomViewController should have been autosized to the top left. Instead, all 6 of the Autosize arrows were on, so it squashed it into funky dimensions.

I know there's no way anyone else could have figured that out by my code, so, have a great day.

-k.

Kevin Beimers