Two Views Error

Discussion in 'iOS Development' started by Steaps, Feb 9, 2009.

  1. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    I'm making a basic application to learn about using two views. I'm looking to change the tint colour of a toolbar from one view (SettingsView), that is located in another view (MainView). Here is my code:

    Code:
    - (void)saveTintColour {
    	MainView *mainView = (MainView *)[UIApplication sharedApplication]
    	if (tintTextField.text == @"Red"){
    		mainView.toolBar.tintColor = [UIColor redColor];
    		NSLog(@"%@", @"Button 1 (Red)");
    	}
    }
    
    And the error:

    Code:
    2009-02-09 15:58:41.630 TheBrowser[787:20b] *** -[UIApplication toolBar]: unrecognized selector sent to instance 0x51fa90
    2009-02-09 15:58:41.631 TheBrowser[787:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIApplication toolBar]: unrecognized selector sent to instance 0x51fa90'
    
    Any help?

    P.S. If i don't have the below lines, the NSLog will appear.
    Code:
    MainView *mainView = (MainView *)[[UIApplication sharedApplication] delegate];
    
    AND
    
    mainView.toolBar.tintColor = [UIColor redColor];
    I know this is wrong, i'm just looking for how to do it correctly.

    EDIT:
    I've been reading through the documentation, can't find anything.
    I could create a ton of extra actions in MainView, then call them through this:
    Code:
    		if([(MainView*)[self view] respondsToSelector:@selector(<Action Here>)])
    			[(MainView*)[self view] <Action here>];
    
    But that would require a lot of unnecessary code.
    Can anyone help please?
  2. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    Wondering, why you are using this

    Code:
    (MainView *)[[UIApplication sharedApplication] delegate]
    This doesn't go to View. It will look in AppDelegate.m file.

    About
    it tell you that toolBar is not exists in UIApplication.
  3. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    I don't know, i'm just really looking for a solution. Can't figure this out.
  4. godogshomie

    godogshomie New Member

    Joined:
    Nov 19, 2007
    Messages:
    558
    Likes Received:
    3
    Code:
    - (void)saveTintColour {
    	MainView *mainView = [[MainView alloc] init];
    	if (tintTextField.text == @"Red"){
    		mainView.toolBar.tintColor = [UIColor redColor];
    		NSLog(@"%@", @"Button 1 (Red)");
    	}
    }
    
    Try this
  5. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    That worked, it didn't change the toolBar's tintColor, but it ran the NSLog. Thanks, now to figure this part out...
  6. godogshomie

    godogshomie New Member

    Joined:
    Nov 19, 2007
    Messages:
    558
    Likes Received:
    3
    do you mind if we see the main view .h
  7. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    Not at all, I'm on my iPod now but will post it tomorrow for sure.
  8. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    you know that will be leaking memory.

    it should be like
    Code:
    - (void)saveTintColour {
        if (tintTextField.text == @"Red"){
            MainView *mainView = [[[MainView alloc] init] autorelease];
            mainView.toolBar.tintColor = [UIColor redColor];
            NSLog(@"Button 1 (Red)");
        }
    }
    
    This would work best if he is saving only red color.

    Was there is a toolbar is defined in MainView?

    There is like self.navigationBar.tintColor or self.navigationController.navigationBar.tintColor too
  9. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    I found the problem, now i'm just trying to figure out how to solve it.
    If i change the tintColor of "toolBar" in "-(void)viewDidLoad", it will change the tintColor.
    If i create a action in MainView, then call the action from SettingsView, it will run the action (I know this through an NSLog), but it will not change the toolBar color. Weird, i don't believe you need to refresh the toolBar, like how you refresh a tableView, am i wrong?

    So basically, it goes like this:
    0. Inside the SettingsView:
    1. Save button pressed: switches view, calls mainView action "toolBarRed".
    2. "toolBarRed" calls a NSLog. Also tries to call/run "toolBar.tintColor = [UIColor redColor];".
    3. The NSLog displays in the debugger, but the toolBar remains the same colour.

    P.S. I have also tried other actions, it will call the actions, but the actions can't change any properties of anything. Just call NSLogs.
  10. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Skimming over this, what you are doing is clearly the problem. Instead of referring to the other view, you are creating a new instance of that other view. So instead of changing the view no the screen, you are changing a view that exists but is not displayed anywhere.

Share This Page