Creating an OSX application without Xcode -
i'm new os x, , i'm trying create simple application without xcode. did found other sites doing that, cannot attach event handlers button.
below code (crafted other sites). creates window , button, don't know how attach event button:
#import <cocoa/cocoa.h> @interface myclass -(void)buttonpressed; @end @implementation myclass -(void)buttonpressed { nslog(@"button pressed!"); //do want here... nsalert *alert = [[[nsalert alloc] init] autorelease]; [alert setmessagetext:@"hi there."]; [alert runmodal]; } @end int main () { [nsautoreleasepool new]; [nsapplication sharedapplication]; [nsapp setactivationpolicy:nsapplicationactivationpolicyregular]; id menubar = [[nsmenu new] autorelease]; id appmenuitem = [[nsmenuitem new] autorelease]; [menubar additem:appmenuitem]; [nsapp setmainmenu:menubar]; id appmenu = [[nsmenu new] autorelease]; id appname = [[nsprocessinfo processinfo] processname]; id quittitle = [@"quit " stringbyappendingstring:appname]; id quitmenuitem = [[[nsmenuitem alloc] initwithtitle:quittitle action:@selector(terminate:) keyequivalent:@"q"] autorelease]; [appmenu additem:quitmenuitem]; [appmenuitem setsubmenu:appmenu]; id window = [[[nswindow alloc] initwithcontentrect:nsmakerect(0, 0, 200, 200) stylemask:nstitledwindowmask backing:nsbackingstorebuffered defer:no] autorelease]; [window cascadetopleftfrompoint:nsmakepoint(20,20)]; [window settitle:appname]; [window makekeyandorderfront:nil]; int x = 10; int y = 100; int width = 130; int height = 40; nsbutton *mybutton = [[[nsbutton alloc] initwithframe:nsmakerect(x, y, width, height)] autorelease]; [[window contentview] addsubview: mybutton]; [mybutton settitle: @"button title!"]; [mybutton setbuttontype:nsmomentarylightbutton]; //set type button want [mybutton setbezelstyle:nsroundedbezelstyle]; //set style want [mybutton setaction:@selector(buttonpressed)]; [nsapp activateignoringotherapps:yes]; [nsapp run]; return 0; }
first of all, don't avoid xcode because beginner. being beginner 1 of many reasons use xcode. resorting fully-manually-implemented code have naive way develop applications os x , you'll encounter far more difficulties worth, non-trivial.
having said that, reason button isn't doing because button doesn't have target. actions require target. in case, want create instance of myclass
class (note class names in objective-c conventionally named in upper camelcase, i.e. myclass
). note action method should take argument (which sender of action), if unused.
- (void) buttonpressed:(id) sender { nslog(@"button pressed!"); //do want here... nsalert *alert = [[[nsalert alloc] init] autorelease]; [alert setmessagetext:@"hi there."]; [alert runmodal]; } // ... myclass *mc = [[myclass alloc] init]; [mybutton settarget:mc]; [mybutton setaction:@selector(buttonpressed:)];
i can't stress enough how ridiculous of code is. bite bullet , dive xcode!
Comments
Post a Comment