Modal Views-problem

Discussion in 'iOS Programming' started by Danneman101, May 24, 2009.

  1. Danneman101 macrumors 6502

    Joined:
    Aug 14, 2008
    #1
    Im trying to implement a modal view but get a number of errors. This is what Ive done so far, and Ive displayed the errors generated in red below the code it referes to:


    1) Created a .xib-file (IntroScreen.xib) using the Interface Builder. Placed one button in it - does nothing, just for show.

    2) In the IB, all Ive done is to write the class-files, producing IntroScreenController.h and .m:

    IntroScreenController.h
    Code:
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    
    @interface UIViewController : NSObject {
        IBOutlet UINavigationItem *navigationItem;
        IBOutlet UITabBarItem *tabBarItem;
        IBOutlet UIView *view;	
        // 
        IBOutlet UIViewController *introScreenController;
    }
    //
    @property (nonatomic, retain) UIViewController *introScreenController;
    
    @end
    
    IntroScreenController.m
    Code:
    #import "IntroScreenController.h"
    [COLOR="#ff0000"]// ERROR: redefinition of 'struct UIViewController'[/COLOR]
    
    @implementation UIViewController
    
    // Synt:	(various)
    @synthesize introScreenController;
    [COLOR="Red"]// ERROR: no declaration of property 'introScreenController' found in the interface.[/COLOR]
    
    @end
    

    3) In the rest of the project (another viewController) I first import the "IntroScreenController.h".

    anotherViewController.m
    Code:
        // Import
        #import "IntroScreenController.h
        [COLOR="#ff0000"]// ERROR: redefinition of 'struct UIViewController'[/COLOR]
    

    4) Then I try to instantiate a introScreenController, and then use the presentModalViewController to display it on top:

    anotherViewController.m -> viewDidAppear()
    Code:
        // Instantiate viewcontroller
        introScreenController *introScreen = [[introScreenController alloc] initWithNibName:@"IntroScreen" bundle:nil];
        [COLOR="#ff0000"]// ERROR: 'introScreenController' undeclared (first use in this function)
        // ERROR: 'introScreen' undeclared (first use in this function)[/COLOR]
    
        // Present modal view
        [self presentModalViewController:introScreen animated:YES];  	
    

    Any ideas?
     
  2. BlackWolf macrumors regular

    Joined:
    Apr 9, 2009
    #2
    very simple. your problem lies here:

    @interface UIViewController : NSObject {

    you are trying to create the class "UIViewController" as a subclass of NSObject. you can't do that because UIViewController already exists as one of apples predefined classes.

    you have to chose a class name that does not yet exist and make that a subclass of UIViewController:

    @interface MyViewController: UIViewController {

    also, you should decide how you write your classnames. normally, classnames are written with an uppercase letter at the beginning. you shouldn't name your file "IntoScreenController.h" if your class is named "intoScreenController" ... but that's just a side note. same goes for navigationItem etc.

    edit: there are numerous other things wrong in your code I just discovered. seems like you really should look over the basics of objective-c again. for example, it seems like you are trying to define a property "introScreenController" which is the class you are currently defining? Also, if you create a subclass of UIViewController, it already has a property "view" (because it is defined in the interface of UIViewController) so you do not need to define it.
     
  3. Danneman101 thread starter macrumors 6502

    Joined:
    Aug 14, 2008
    #3
    Thanks for clearing a few things up for me. Ive edited the code according to your suggestions (in blue below), and the code does now compile without any errors.

    In runtime, however, it crashes due to an unhandled exception. Shouldnt this code be valid?


    IntroScreenController.h
    Code:
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    
    @interface [COLOR="#0000ff"]IntroScreenController : UIViewController[/COLOR] {
    [COLOR="#0000ff"]// removed everything, since I dont need them[/COLOR]
    }
    [COLOR="#0000ff"]// removed property[/COLOR]
    @end
    

    IntroScreenController.m
    Code:
    #import "IntroScreenController.h"
    
    @implementation [COLOR="Blue"]IntroScreenController[/COLOR]
    
    [COLOR="#0000ff"]// removed synthezise[/COLOR]
    
    @end
    

    anotherViewController.m
    Code:
        // Import
        #import "IntroScreenController.h
    

    anotherViewController.m -> viewDidAppear()
    Code:
        // Instantiate viewcontroller
        [COLOR="#0000ff"]IntroScreenController[/COLOR] *introScreen = [[[COLOR="Blue"]IntroScreenController[/COLOR] alloc] initWithNibName:@"IntroScreen" bundle:nil];
     
        // Present modal view
        [self presentModalViewController:introScreen animated:YES];  	
    
     
  4. BlackWolf macrumors regular

    Joined:
    Apr 9, 2009
    #4
    the code you posted seems right ... the error has to lie somewhere outside of the code you provided.
    you are sure the nib "IntroScreen" exists?
     
  5. Danneman101 thread starter macrumors 6502

    Joined:
    Aug 14, 2008
    #5
    Well, it should exist. There is a file called "IntroScreen.xib" in the project, and double-clicking it opens the controllview in IB. Do I need to do something else to register the file with the project?
     
  6. BlackWolf macrumors regular

    Joined:
    Apr 9, 2009
    #6
    normally not. then the error has to be outside of the code you posted.
     
  7. Danneman101 thread starter macrumors 6502

    Joined:
    Aug 14, 2008
    #7
    Strange. It seems to be the last line that triggers the exception:

    Code:
    [self presentModalViewController:IntroScreen animated:YES]; 
    
    Blanking that line out will make the code work as normal.
     
  8. BlackWolf macrumors regular

    Joined:
    Apr 9, 2009
    #8
    set a breakpoint and go through your app line for line and see when the exception is raised.
     
  9. Danneman101 thread starter macrumors 6502

    Joined:
    Aug 14, 2008
  10. BlackWolf macrumors regular

    Joined:
    Apr 9, 2009
    #10
    mh ... maybe I'm blind but I don't see anything obviously wrong with your code.

    1) post your "viewDidAppear" method completly please, exactly the way it is in your code.

    2) try
    Code:
    [introScreen retain]
    before the line that causes the exception. I don't think that this will help, I just want to make sure it's not a memory managment problem

    3) did you overwrite any of the init-methods (init, initWithNib, initWithFrame) in IntroScreenController? if so, post the method you wrote please

    I'm guessing that something is wrong with your IntroScreenController class somehow, so it doesn't do what presentModalViewController expects it to.
     
  11. Danneman101 thread starter macrumors 6502

    Joined:
    Aug 14, 2008
    #11
    1) Actually, I have no other code than those two lines.

    2) Same problem even with that code.

    3) The IntroScreenController doesnt contain any other code than the code displayed above.


    Just to understand things correctly, the nib-name in the initWithNibName-funktion must be exactly the same as the name of the .xib-file, right?

    And the presentModalViewController-function can (as the name suggests) only present UIViewControllers and not just regular UIViews, right?
     
  12. Danneman101 thread starter macrumors 6502

    Joined:
    Aug 14, 2008
    #12
    Well, since there was no apparent solution, I just made a uiview and loaded it with addsubview instead. It is an intro-screen that only displays once (checked by setting-variable), and has a full screen button with a backgroundimage that closes the view on press. Also, it plays a backgroundsound.

    Code:
    - (void)applicationDidFinishLaunching:(UIApplication *)application
    {
    	// --------------------------
    	// SET:		IntroScreen
    	// --------------------------
    	
    	// Get Handler
    	// -----------
    	//AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];	
    	
    	// Kolla med settings om första gången.
    	NSString *setting_on = [[NSUserDefaults standardUserDefaults] stringForKey:@"settings_intro_on"];
    	if ([setting_on isEqualToString:@"no"]) {}
    	else
    	{
    		// View	
    		// -----
    		CGSize screenSize = [UIScreen mainScreen].bounds.size;
    		CGRect screenBounds = CGRectMake(0, 0, screenSize.width, screenSize.height);	
    		introView = [[UIView alloc] initWithFrame:screenBounds];			
    	
    		// Add Button
    		// ----------
    		UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    		[closeButton setFrame:CGRectMake(0.0f, 0.0f, screenSize.width, screenSize.height)];
    		// Add image
    [closeButton setBackgroundImage:[[UIImage imageNamed:@"introImage.png"] stretchableImageWithLeftCapWidth:110.0 topCapHeight:0.0] forState: UIControlStateNormal];
    		// Add close function
    		[closeButton addTarget:self action:@selector(CloseIntro) forControlEvents:UIControlEventTouchUpInside];	
    		// Add to View
    		[introView addSubview:closeButton];
    	
    		// TODO:	Add sound-off-button
    						
    		// Play sound	
    		// ----------
    NSString *path = [[NSBundle mainBundle] pathForResource:@"introSound" ofType:@"mp3"];   
    		introAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];   
    		introAudio.delegate = self;   
    		[introAudio play];  
    				
    		// Show All
    		// --------
    		[window addSubview:introView];
    			
    		// Set setting = NO	
    		// ----------------
    		[[NSUserDefaults standardUserDefaults] setObject:@"no" forKey:@"settings_intro_on"];
    	}
    }
    -(void) CloseIntro
    {
    	// TODO:	alt.1) Fade-out-close,		alt.2) Flip-close
    	
    	// Stop sound
    	// ----------
    	[introAudio stop];
    	
    	// Close Intro
    	// -----------
    	[introView removeFromSuperview];
    }
    
    Thanks for trying to help out, BlackWolf :)
     

Share This Page