IUButton problem in IB unknown selector

Discussion in 'iOS Programming' started by shinta187, Sep 27, 2010.

  1. shinta187 macrumors newbie

    Joined:
    Sep 27, 2010
    #1
    Hi everybody,

    Im new here, i recently started programming for the ipad/iphone.
    Im trying to make an iPad Book Application.

    I am using a simple button, i hooked everything up in IB, created outlets etc.

    But when i click the button(setttings button upper right corner), the app crashes and it says, "unknown selector etc.."

    It doesn't even get in the function. I don't even see my "NSLog(@"do something");"

    I have selected my file owner in IB to my class (HomeView) where all my outlets are and my functions.

    It's the same with the pageViewControls for my slider.
    When i set the delegate to "self" and i click.. it crashes.

    Below is my project for download. i commented the delegete out. since it crashes. (NDC2.zip)

    Can anybody please help? It's driving me nuts for the past 2 days.

    Thank You

    http://www.mediafire.com/?3r02qifcxxz3ql7
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    The details of the error message are quite helpful if you expect us to help troubleshoot. Be specific. Don't use "etc."
     
  3. ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #3
    When I downloaded it, it didn't have HomeView.xib in it. Couldn't compile
     
  4. shinta187 thread starter macrumors newbie

    Joined:
    Sep 27, 2010
    #4
    error message

    Thanks for the quick reply,

    Strange that homeview.xib is not included, i downloaded the file myself from mediafire and it is included.
    ============================================
    The error is get is this:

    2010-09-27 21:11:45.507 NDC2[1251:207] *** -[NSCFType doSomething:]: unrecognized selector sent to instance 0x4d15570

    2010-09-27 21:11:45.509 NDC2[1251:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFType doSomething:]: unrecognized selector sent to instance 0x4d15570'

    ============================================

    Can you try again? Thank you.
     
  5. Sykte macrumors regular

    Joined:
    Aug 26, 2010
    #5
    If this button was created in the navigation bar did you add a target/action for it.

    If it wasn't and it's a button you created in IB then check your outlet.

    Code:
    -(IBAction)doSomething:(id)sender;
     
  6. shinta187 thread starter macrumors newbie

    Joined:
    Sep 27, 2010
    #6
    The button was created in IB, i don't have a navigationBar.

    The outlet is my HomeView class .h (even though it doesn't need one )and in the .m is my function, i synthesized it and i connected the button in IB.

    So i think the class is set up properly. tomorrow i will post the exact code.

    thanks for the reply.
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Yes, please. That is almost always the best idea. If you don't want to give too much away, create a test project that recreates the issue and post that.
     
  8. shinta187 thread starter macrumors newbie

    Joined:
    Sep 27, 2010
    #8
    Here is the code.


    AppDelegate.m i have

    Code:
    #import "AppDelegate_iPad.h"
    #import "HomeView.h"
    
    @implementation AppDelegate_iPad
    
    #pragma mark -
    #pragma mark Application lifecycle
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    	
       	HomeView *homeview =[[HomeView alloc]
    						 initWithNibName:@"HomeView"
    						 bundle: nil];
    	
    	[window addSubview:homeview.view];
    	[window makeKeyAndVisible];
    	
    	[homeview release];
    	
    	return YES;
    }
    
    
    In my HomeView.h i have:

    Code:
    #import <UIKit/UIKit.h>
    
    @interface HomeView : UIViewController <UIScrollViewDelegate> {
    	
    	UIScrollView *scrollView;	
    	UIPageControl *pageControl;
    	UIButton *homeButton;
    	UIButton *shopButton;
    	UIButton *priceButton;
    	UIButton *settingsButton;
    
    	BOOL pageControlIsChangingPage;
    }
    
    @property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
    @property (nonatomic, retain) IBOutlet UIPageControl *pageControl;
    @property (nonatomic, retain) IBOutlet UIButton *homeButton;
    @property (nonatomic, retain) IBOutlet UIButton *shopButton;
    @property (nonatomic, retain) IBOutlet UIButton *priceButton;
    @property (nonatomic, retain) UIButton *settingsButton;
    
    - (IBAction)changePage:(id)sender; //does not work
    - (IBAction)doSomething:(id)sender; // does not work
    
    @end
    
    in my HomeView.m

    Code:
    #import "HomeView.h"
    
    
    @implementation HomeView
    
    @synthesize scrollView;
    @synthesize pageControl;
    
    
    ///////////////////
    //////Buttons//////
    ///////////////////
    
    @synthesize homeButton;
    @synthesize shopButton;
    @synthesize priceButton;
    @synthesize settingsButton;
    
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    
    	return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);	
    }
    
    #pragma mark -
    #pragma mark UIView boilerplate
    - (void)viewDidLoad 
    {
    	
        [super viewDidLoad];
    	
    	scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    	scrollView.clipsToBounds = YES;
    	scrollView.scrollEnabled = YES;
    	scrollView.pagingEnabled = NO;
    	scrollView.showsVerticalScrollIndicator = NO;
    	scrollView.showsHorizontalScrollIndicator = NO;
    	
    	//scrollView.delegate = self; 
    	
    	NSUInteger nimages = 0;
    	CGFloat cx = 0;
    	for (; ; nimages++) {
    		NSString *imageName = [NSString stringWithFormat:@"book%d.png", (nimages + 1)];
    		UIImage *image = [UIImage imageNamed:imageName];
    		if (image == nil) {
    			break;
    		}
    		UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    		
    		CGRect rect = imageView.frame;
    		rect.size.height = image.size.height;
    		rect.size.width = image.size.width;
    		rect.origin.x = ((scrollView.frame.size.width - image.size.width) /2) + cx;
    		rect.origin.y = ((scrollView.frame.size.height - image.size.height) / 2);
    		
    		imageView.frame = rect;
    		
    		[scrollView addSubview:imageView];
    		[imageView release];
    		
    		cx += scrollView.frame.size.width / 2;
    	}
    	
    	self.pageControl.numberOfPages = nimages;
    	[scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
    }
    
    #pragma mark UIScrollViewDelegate stuff [B]// here it is going wrong[/B]
    - (void)scrollViewDidScroll:(UIScrollView *)_scrollView
    {
        if (pageControlIsChangingPage) {
            return;
        }
    	
    
        CGFloat pageWidth = _scrollView.frame.size.width;
        int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
        pageControl.currentPage = page;
    }
    
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView 
    {
        pageControlIsChangingPage = YES;
    }
    
    
    #pragma mark -
    #pragma mark PageControl stuff
    - (IBAction)changePage:(id)sender 
    {
        CGRect frame = scrollView.frame;
        frame.origin.x = frame.size.width * pageControl.currentPage;
        frame.origin.y = 0;
    	
        [scrollView scrollRectToVisible:frame animated:YES];
    	 
        pageControlIsChangingPage = YES;
    }
    
    - (IBAction)doSomething:(id)sender  // doesn't work
    {
    	NSLog(@"do something");
    }
    
    
    
    - (void)didReceiveMemoryWarning 
    {
        [super didReceiveMemoryWarning];
    }
    
    - (void)viewDidUnload 
    {
    	[homeButton release];
    	[shopButton release];
    	[priceButton release];
    	[settingsButton release];
    }
    
    
    - (void)dealloc 
    {
    	[scrollView release];
         [super dealloc];
    }
    
    @end
    
    


    In my nib file (HomeView) i have my file owner at HomeView class, connected the button. but still crashes,

    to sum it up:

    I see my books in my slider. slider itself works except the pageControl.
    when i want an action on one of the buttons is crashes. doesn't matter what i put in the function.

    I hooked the settings button up in IB.

    Thank's for the quick replies
     
  9. shinta187 thread starter macrumors newbie

    Joined:
    Sep 27, 2010
    #9
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Which button? You have four defined, one of which is not an IBOutlet.

    Crashes how? Be specific. Are you still getting the run-time error you posted earlier? If not, what are you getting now? If nothing, have you checked the crash log?

    Also, just a word of advice, if you're going to subclass UIViewController (which HomeView is), you should name it nameViewController so that it does not seem as though it is subclassed from UIView. Both of these classes are subclassed frequently (although UIViewController much more so), so better naming will help you and us to be less confused, hopefully.
     
  11. shinta187 thread starter macrumors newbie

    Joined:
    Sep 27, 2010
    #11
    Alright thanks for the tip.
    ==========================================
    i just started a new universal project.
    so all i got was 2 files AppDelegate_Ipad.h and AppDelegate_Ipad.m

    then i added a new uiviewsubclass with a xib file.. (so 3 new files .h .m and a xib file)

    then i began to work with it and added it to the window.
    ==========================================

    The programs just crash with the same error i was getting before. so the simulator goes back to the homescreen.

    I want the settingsButton to do something. so i connected it in IB, the rest of the buttons i just defined, i want to do something with them later.

    If you can download the project you see what i mean.

    thank you.
     
  12. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    Yeah, seems to me that your settingsButton is being released before you can click on it to call doSomething:. But I can't, for the life of me, figure out why. Strange, indeed. Probably time to enable zombies.
     
  13. shinta187 thread starter macrumors newbie

    Joined:
    Sep 27, 2010
    #13
    That is what i was thinking as well.. that it's being releases before calling.

    i can't figure it out :S.
     
  14. shinta187 thread starter macrumors newbie

    Joined:
    Sep 27, 2010
    #14
    hm,, when i remove the line "[homeView release]" in appDelegate_Ipad.m it works... the settingsButton gives me the output "Do Something".

    strange....


    Thanks for going through it with me.
     
  15. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #15
    Not so strange. Why did you think you needed to put this release there?
     
  16. shinta187 thread starter macrumors newbie

    Joined:
    Sep 27, 2010
    #16
    ups..

    confused the file with another..

    it all makes sense now, i'm still new at this, so i'm trying my best.

    thanks again.

    (you must be thinking. "God, those Noobs...") :p thanks.
     
  17. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #17
    You're welcome.

    Actually, I was thinking: "Yay, they figured it out on their own!" I always feel that solving problems like this, where you come to your own discovery as to the cause only help to truly understand and stick in your memory for the next time something similar happens.
     
  18. shinta187 thread starter macrumors newbie

    Joined:
    Sep 27, 2010
    #18
    You got that right.

    I will never forget this again :)

    Anyway i can go on with my work now. If i have a problem. You'll be the first to know ;)

    Have a great evening.
     
  19. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #19
    P.S. Don't forget to be a "good memory-citizen" and release homeview at some point where it is no longer needed.
     
  20. shinta187 thread starter macrumors newbie

    Joined:
    Sep 27, 2010
    #20
    yes i will do that, probably in ViewDidUnload.

    thanks
     

Share This Page