viewController and Brain Melt

Discussion in 'iOS Programming' started by IDMah, Nov 8, 2011.

  1. IDMah, Nov 8, 2011
    Last edited: Nov 9, 2011

    IDMah macrumors 6502

    IDMah

    Joined:
    May 13, 2011
    #1
    Hi all.

    I made a mess of my App.

    all my buttons are created in Window. which also has a xib.
    I have a UIView to interpret Shakes.

    BUT ! everything works fine. All the buttons work, shake and gestures.
    but I want to do things "right" and tried to implement a ViewController.

    Where I add the shakeView.

    in Delegate gameMainDelegate:
    Code:
    UIViewController *shakeViewControll = [shakeViewController alloc] init];
    	[[self window] addSubview:shakeViewControll.view];
    	[shakeViewControll release];
    
    in Controller shakeViewController:
    Code:
    - (id)init
    {
    	// initlize my view controller // 
    	NSLog(@"starting controller");
    	
    	
    	return self;
    }
    
    -(void)viewDidLoad
    {
    	[super viewDidLoad];
    	self.wantsFullScreenLayout = YES;
    	
    	shakeView *VD = [[shakeView alloc] init];
    	[self.view addSubview:VD];
    	
    	NSLog(@"view did load");
    }
    	
    
    -(void)loadview
    {
    	// creates the view // 
    	NSLog(@"load View");
    }	
    
    But nothing happens when. also all my buttons I created don't trigger.
    including the xib ones. but if I just slap the view on the window everything is copacetic.

    ala from gameMainDelegate:
    Code:
    	shakeItView = [[shakeView alloc]initWithFrame:wholeWindow];
    	[shakeItView setBackgroundColor:[UIColor clearColor]];
    	[[self window] addSubview:shakeItView];
    
    everything works fine.

    I know I have to pass the gesture back up the chain or something. Is there a good example of this ? apple developer docs make my brain bleed...

    Basically I'm piling all my views on top of one another on the window.
    I don't need to switch views, I want my swipe gestures and shakes feed to the model and my buttons to be updated when those gestures/shakes are triggered.

    How would someone (you) smarter than me structure this?

    thanks in advance.
    Ian
     
  2. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    Code:
    UIViewController *shakeViewControll = [shakeViewController alloc] init];
    That made MY brain bleed ;) Class names are named with Capitals, just do that in the future. That was PRO TIP 1 for free!
    Next thing is, you make an UIViewController of what? what is that class? What does it define, does it inherit UIViewController?
    What will the rest of the app do, will it be in tabs, or via navigation, or what? because that's the first step.
    You need to break up your logic in seperate steps of what you want your app to do.

    But, I see, you add a View as a subview to your ViewController. okay. That could be just an outlet rather in your xib. but adding is okay too.

    But, you add new controllers to your Main, that's in my opinion wrong, you need to add your navigation/Tabbar there, and go on from there, and first think out of your flow, what you want it to do, because. Can't really give good advise on that without knowing more.
    What I would do if I was you, is check out the Stanford University video's, sometimes they can be a bit boring, but you will learn exactly what all your questions are. And it's awesome learning material.
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    A view controller's view is created in its loadView method. The loadView method you show doesn't create a view. You should either not implement loadView if it does nothing. Or call super loadView to let UIViewController create the view. Or your implementation should create a view and assign it to self.view.

    If all it does is the code you show I would use a nib anyway and then call initWithNibName.
     
  4. dejo, Nov 9, 2011
    Last edited: Nov 9, 2011

    dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    That may be but your code for each approach is rather different. Why would you expect the same result?
     
  5. IDMah thread starter macrumors 6502

    IDMah

    Joined:
    May 13, 2011
    #5
    tried and clarified; maybe.

    Thanks everyone..

    Basically the game is moving pieces around a board.
    I wanted an overlay text for undo - redo and reset using gestures.
    which I put under the buttons, which I make semi transparent.

    So I want one game screen no navigation tabs..

    I did try: in shakeViewController

    Code:
    -(void)loadView
    {
    	[super loadView];
    	// creates the view // 
    	NSLog(@"load View");
    	
    	self.wantsFullScreenLayout = YES;
    	
    	shakeView *VD = [[shakeView alloc] init];
    	[self.view addSubview:VD];
    	
    	//[VD release];
    	
    }
    
    Still nothing. I was under the impression that if I do.

    Code:
    shakeView *VD = [[shakeView alloc] init];
    [self.view addSubview:VD];
    I was creating and adding the view to my viewcontroller.

    Should I be putting the gesture and shake recognizer in viewcontroller?
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    You need to debug this.

    What is the value of self.view in loadView? What is the frame of your view that you add. If you set the background color of these views to green or red can you see them. ETC.

    Start from the beginning. Go step by step. Check every variable for reasonableness. When you find something that isn't right fix it and start over.

    You need to learn how to do this.
     
  7. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #7
    Did you look at the docs?

     
  8. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #8
    Instead, you should use the ViewDidLoad function, since that gets triggered whenever your NIB is loaded.
    Frikking tadaaa. I'm sorry, it's a matter of reading docs and a tad of iPhone development, if you don't read up on what you want to do, and rush it heads on, you will be left empty-headed.
    I was like that too, till I bumped into iPhone development, it's different ALOT vs Java or something similair, so you will really need to start reading things.
     
  9. IDMah thread starter macrumors 6502

    IDMah

    Joined:
    May 13, 2011
    #9
    thanks jnoxx

    Yeah I guess I've been doing what I do in life, Stumble through..
    thanks for the encouragement.

    Well here's an update and some useful stuff I found after countless google searches:

    So I wanted to ignore all taps. Passing the input back to my window.
    (soon to be a UIView and ViewController)

    http://vectorvector.tumblr.com/post/2130331861/ignore-touches-to-uiview-subclass-but-not-to-its

    so in the shakeView I added:
    Code:
    -(id)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
        id hitView = [super hitTest:point withEvent:event];
        if (hitView == self) return nil;
        else return hitView;
    }


    which according to the web: "To ignore touches in a UIView subclass, but not its subviews, just work a little hitTest magic:"

    also thanks to PhoneyDeveloper I finally added fixed in shakeViewController:

    Code:
    -(void)loadView
    {
    	NSLog(@"load View");
    	CGRect fullScren = [[UIScreen mainScreen] bounds];
    	dealWithItGestureView *myView = [[dealWithItGestureView alloc] initWithFrame:fullScren];
    	[myView setBackgroundColor:[UIColor clearColor]];
    	self.view = myView;
    	[myView release];	
    }
    realizing that I didn't define the size of my view. now just have to get it to recognize the shakes.
    Should I be doing that in the ViewController ? or the UIView? neither seem to be working for me..

    thanks all
    Ian
     
  10. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #10
    One word of advice. The way to learn iOS development, and most other things, is to follow a course of study. That means get a book and work through it start to finish or use the Stanford lessons or something like that, start to finish. Google searches for info work when you can understand the results. Coming onto a forum and asking questions where you don't understand the answers isn't the way.

    Also, read the sticky for this forum about sources of info. It's simply not reasonable to expect that you can learn by coming onto a forum every time you have a question. And the google man will give you as many wrong answers as right answers, when you can't tell the difference.
     

Share This Page