Not signed in (Sign In)

Vanilla 1.1.8 is a product of Lussumo. More Information: Documentation, Community Support.

    • CommentAuthorSneWs
    • CommentTimeFeb 1st 2008
     
    I'll be forever grateful for the sources/help I can get. I'm working as a software engineer and we are now planning on porting our applications to OS X, but I have some issues finding info on how to build a GUI from ground up NOT using the interface builder and nib files, our GUI's are advanced and are highly dependent on states in the application.

    I have been googling around for a couple of days and haven't found anything else then intros on how to use the Interface builder to do the GUI stuff, if anyone can supply me some information on where to start or some sources / how to / Podcasts on how to do this I'll be more then grateful.

    Tanks.
    • CommentAuthorgracion
    • CommentTimeFeb 1st 2008
     
    Even with a dynamic interface, Interface Builder is usually a good place to start. I mean, you'll have a window and certain menus, right? You can add and remove items in code as the nib (Interface Builder-made resource file) opens (-awakeFromNib). You can can put sub-parts, such as a control inside a view, as separate parts in a nib file and dynamically put them into a window.

    Check the class reference documents (Built into Xcode, see the Help menu) for the various UI classes: NSWindow, NSView, NSControl, NSMenu, NSMenuItem, etc (you can see the class names in Interface Builder). Each class reference has some description at the top that may discuss dynamic use. There's usually a link to the relevant "Guide" document at the top of a class reference, for more conceptual and tutorial-style documentation.
    • CommentAuthorSneWs
    • CommentTimeFeb 1st 2008 edited
     
    Graciac gracion ;)

    I will check it out, but maybe you could answer me this?

    If I load the NSApplication using the sharedApplication: method and then call the runModalForWindow: wnd

    is this the way to do it if I want to code the entire gui? or do you have something else to recommend?

    *** EDIT

    I may add that I'm used to program in QT and C++ and I always code the GUI's from ground up, even if it's a simple GUI or not, this is to maintain the best knowledge possible of the Framework and the GUI code since it's not in my nature to code something and not knowing what's happening in the background. I'll might be old fashion, but it has kept our software in shape and it's always easy to maintain since we can apply a single coding standard and everyone knows where to look etc even when we switch between frameworks / languages.

    And yes, we could use QT for this, but since it's going to be a native OS X application we have made the decision to go with Cocoa and Objective-C

    Tanks /SneWs
    • CommentAuthortwentyeight
    • CommentTimeFeb 1st 2008 edited
     
    I can understand and appreciate where you're coming from in your motivation to code the gui from the ground up, but I feel obligated to submit my two cents…
    One of the biggest usability strong points that OS X (largely due to Cocoa) capitalizes on is consistency. One of the biggest caveats to using, say, a Java-based (Swing/AWT) desktop app is its lack of consistency. As a user, inconsistency (often through hand-coding UI aspects) severely inhibits my ability to react to various states the application may present, and so as a developer I avoid it at all costs. Interface Builder may feel a little too automated (I'm personally not very comfortable drag&dropping an NSToolbar in yet as opposed to hand-coding) but it does form a very solid and consistent base to build upon programatically, and is the preferred way of doing so.

    At any rate, what you're probably looking for is a healthy mixture of addSubview: and setContentView:

    -adc
  1.  
    Just to throw my 2¢ worth in as well...

    I used to code a lot of GUI stuff when I first started programming the Mac back in the 80s. At first it felt good because I was "in control" except after a while just to get something good looking I'd end up spending vast amounts of time tweaking things pixel by pixel (or else leave it looking like a dog's dinner).

    Once I let go a bit and used ResEdit (as it was then) to lay out my GUIs the appearance of my apps improved dramatically and the time spent on coding the GUI dropped dramatically as well. I've never looked back.

    Interface Builder is infinitely better than ResEdit ever was, and there's just no good reason not to use it. You might feel that you are giving up too much to code working behind your back that you cannot see, but in fact that's not really the case - all that loading a NIB does is all the chores of creating and positioning all your controls, then hooking them up to your data members and action methods (OK, more than that if you use bindings, but let's keep the discussion simple). As far as I can see IB does not restrict you very much, except in the sense of going with the flow instead of going against it, which is a good thing anyway.

    Using IB does not in any way prevent you from creating a "dynamic interface", whatever that means in your terms. You can use IB to set up entire views, or parts of views, just as much as complete window layouts, and you can write code to make use of those smaller pieces if you so wish. A typical trick for switching views I use is to use a tabless tab view and simply lay out each tab in IB then the code switches the invisible tabs to select different views.

    I cannot think of any programming task where IB would hinder more than help, so I strongly recommend not trying to avoid it. Handling all that GUI stuff yourself is exceedingly tedious, won't look good without a great deal of time spent, and will be much more difficult to maintain, localise and tweak than using IB.
    • CommentAuthorSneWs
    • CommentTimeFeb 2nd 2008
     
    Tanks to both twentyeight and Graham_Cox.

    I do respect and understand the points you bring up, but it still not good enough for me. I don't want to load a nib file containing a lot of controls where just some of it will be used, what happened to optimize your apps and not load more than needed for the moment to improve overall speed and memory usage. (Yeah, more nib files, but still, that's not what I want ;)

    Regarding the points brought up by Graham_Cox that the GUI would look like a "dog" and not be consistent, I don't agree with that, you use layout containers and lightweight algorithms to properly align and position/size controls. Don't now how apple has solved this in Cocoa, but I'm looking at the documentation and reading up on the subject, there has to be something that helps dev's with that.

    So finally, anyone have any resources on how to use cocoa and Objective-C to hand code GUI's?
    • CommentAuthorgracion
    • CommentTimeFeb 2nd 2008
     
    SneWs,

    Sorry, -runModalForWindow is for temporarily putting up an alert, and only in rare cases where the user must be forced to respond to the alert before doing anything else. Notice the 1st discussion paragraph in the ref doc about how the entire app UI is disabled.

    At minimum, you can put the minimum UI items that you will always use (a blank window, the basic menus) and you will not be wasting any memory (and saving time). Then, in your application delegate class (the top-level controller class in MVC), have an instance variable connected (yes in Interface Builder) to your window in the nib. Use that reference to add UI items programmatically--there's your hand-coding, and plenty of it. It won't be as efficient as having the items you normally use in the nib file, and will take longer to create, debug, and maintain.

    Also, you could programmatically instantiate an NSWindow, populate it with views and controls, then I think you just call -makeKeyAndOrderFront (don't hold me to that). But you're really going to need to know a lot more about low-level stuff: event loop, responder chain, how views work, etc.

    As suggested by the comments, no experienced Cocoa programmer who would do away with Interface Builder and nib files. Even though this is different than your standardized approach, coding to a native framework requires specialized knowledge anyway. Put comments in your code like "See main.nib for starting menu contents and windows".

    Apple (and NeXT) engineers have spent many years optimizing and debugging the frameworks which the nib file resources leverage. While you are obviously an expert programmer, it would be quite difficult to improve on this from an optimization standpoint. Also, this is premature optimization.

    As I see it you have 3 paths to choose from:

    1. Base your implementation on the tutorials using nib file resources, the proven, robust, efficient, easy way to create Mac apps. You can still avoid the extra nonprogrammatic stuff like bindings (although they're efficient and easy) and Core Data. Skim over the Cocoa Fundamentals Guide to get your bearings. "The code you don't have to write is the code you don't have to debug and maintain."

    2. Spend weeks studying the low-level Mac app implementation, including a throughrough read of the Human Interface Guide, Cocoa Fundamentals Guide, the Objective-C Programming Language (for the runtime), probably the Model Object Implementation Guide, and of course all the Application Kit reference docs for all the programmatic methods you'll need to replicate what IB does for you. (I'm not trying to be hyperbolic--its just that you'd be reinventing a huge wheel.)

    3. Pick out a few classes and methods (well, like -runModalForWindow) and create some kind of non-Mac interface that Mac users will hate. Not just because it's different, but because they won't be able to figure it out, and those that figure it out won't be able to use it in the ways they want.

    SneWs, I appreciate the polite approach you've taken and your willingness to tackle a new platform. We welcome you to the Mac developer community. Like Graham, some of us date back to pre-resouce days, but we think Cocoa rocks, and we think you'll dig it too. Maybe it's time to let go of some ideas that work for you elsewhere, and consider a different approach.
    • CommentAuthorgracion
    • CommentTimeFeb 2nd 2008 edited
     
    This post mentions an cross-platform app, Transmission, using Cocoa. You can get the source and see what they did:
    http://theocacao.com/document.page/548
    • CommentAuthorSneWs
    • CommentTimeFeb 2nd 2008
     
    One again gracion, tanks.

    I have spent the last 48h on coding with cocoa and setting up some test projects etc, I found a simple way to actually skip the designer stuff, using the setDelegate: to a custom NSApplication pointer, BUT it sure is a lot simpler to use the awakeFromNib: and hook it from the Interface builder, I have actually starting to get used to the approach using IB. ( Will probably do some tests using designer generated views etc to ) =)

    - "Maybe it's time to let go of some ideas that work for you elsewhere, and consider a different approach."
    Yeah, can't argue with that =)

    I feel that this is going to be one of the most exciting steps in my career as a developer, not just because it's a new platform, but because it's a very rich framework and it provides a gorgeous UI from start.

    gracion, really appreciate the answers you have provided me with, they have been really helpful. And now, it's back to the documentation and XCode.
  2.  
    "what happened to optimize your apps and not load more than needed for the moment to improve overall speed and memory usage"

    This is spurious, I think. Until you try both approaches and compare them with profiling tools, you cannot make any "intuitive" judgement about memory and/or speed in advance. Whether you let controls come from a NIB or by instantiating them in code, you end up with the same memory usage since you have the same objects in memory. The NIB approach is likely to require less code, so overall the memory usage will likely be lower.

    "you use layout containers and lightweight algorithms to properly align and position/size controls. Don't now how apple has solved this in Cocoa"

    Apple has solved this using Interface Builder. There are no APIs in Cocoa that give you layout abilities beyond positioning things to an x,y co-ordinate and a width,height size. The tools that allow you to conform to the Aqua layout guidelines are built into IB, not into Cocoa.

    One of the things you're likely to run into, whether you like it or not, right or wrong, is that Mac users are very picky about their apps and have certain expectations. If you don't meet them, your app will fail in the Mac marketplace. (I'm assuming it will be a public app and not something private to a particular org). These expectations go beyond the appearance, though that's important. It's also about natural "Mac-like" behaviours, and in may cases the ability to go into the app itself with tools like IB. There are several legitimate reasons to do this, not least of which is localisation. If you use IB correctly and fully, your app is localisable without any requirement to reveal or change your source code, or any recompilation. This is a powerful and useful feature which you'd be giving up by coding your GUI.
    • CommentAuthorSneWs
    • CommentTimeFeb 2nd 2008
     
    Graham_Cox, tanks for your comment, as in earlier discussion with both you and gracion I have had some time to continue try stuff out.

    As I come from the world of C++ and QT/KDE and C#.NET (Windows/Mono) I'v grown used to code GUI stuff by hand, even if .NET provides form designers etc they are not needed and in QT you also have GUI designer tools but it's just a code generator.

    I have never liked to work with thinks I don't understand, I have been taught that code generators is good as long as you understand what they do and generate so that you can fix bugs, optimize the generated code if needed (have been fortunate enough to not run into those problems as often as one can think).

    "Until you try both approaches and compare them with profiling tools, you cannot make any "intuitive" judgement about memory and/or speed in advance"

    You are def. right about that, the thing I had in mind writing that was that you can improve overall memory usage if you don't need to instantiate controls/objects until they are needed, and in GUI programming that can mean great startup time improvements etc. Of course this is something you need to test and tweak from case to case.

    "One of the things you're likely to run into, whether you like it or not, right or wrong, is that Mac users are very picky about their apps and have certain expectations"

    This is something I actually like, I my self is very picky about applications look and feel, and Apple is the reference for great looking GUI's even when I do windows, Linux applications, the soft touches and the simplicity is just overwhelming.

    I must say that I'm very impressed by the answers I have gotten from this thread, you have been very helpful =)

    * Side note:
    The application I'm planning will be for the business segment including PBX / PAM stuff. Theirs no timeline for this project, still in R&D.
    • CommentAuthoriluvcapra
    • CommentTimeFeb 4th 2008
     
    "I have never liked to work with thinks I don't understand, I have been taught that code generators is good as long as you understand what they do and generate so that you can fix bugs, optimize the generated code if needed (have been fortunate enough to not run into those problems as often as one can think)."

    It might be relevant to point out that IB is not generating code, it's letting you insantiate objects and is then serializing them into the nib/xib file for you. Nibs are not executed, they are decoded into memory. If you wanted to have strict control over how your UI was getting built, like the control XAML might give you under Vista (just threw up a little) you could use an XML nib and create it in a text editor.

    If you wanted to write a IU from the ground up in code, you might take a look at the Carbon API; even though this is not Obj-C, it might be alot easier to find code samples and knowledgebase in the "UI from scratch camp," and it'll probably be easier for you to do some of the stuff that Cocoa really discourages you from, like, say, subclassing NSApplication, or implementing your own run loop, things that are theoretically possible but very difficult to do in a way that'll be compatible on the OS X platform long-term, let alone in a way that is efficient with your time.

    If you merely want to know how a Cocoa application runs and want to make sure you're not glossing over stuff that'll bite you, obviously a Cocoa application is just a call to NSApplicationMain(), and if you want to see how that's implemented, you can see the GNUStep implementation here :

    http://www.google.com/codesearch?hl=en&q=show:FJffXv3_T6U:fPvp4g4vcg8:ICnbE4wOOgI&sa=N&ct=rd&cs_p=ftp://ftp.gnustep.org/pub/daily-snapshots/core.current.tar.bz2&cs_f=core/gui/Source/Functions.m&start=1

    And this just has a fancy way of calling [NSApplication run], which you can see here, starting at line 1327:

    http://www.google.com/codesearch?hl=en&q=show:eOhRdyymt74:fPvp4g4vcg8:Q6xjB8scXvM&sa=N&ct=rd&cs_p=ftp://ftp.gnustep.org/pub/daily-snapshots/core.current.tar.bz2&cs_f=core/gui/Source/NSApplication.m&start=1

    all [NSApplication run] does is spin a while loop around the event getting, with a quit at the end and [self finishLaunching] at the beginning. [self finishLaunching] fails if you don't have a nib, which I think indicates just how embedded nibs are into the Cocoa model for development.

    I'm sorry for wasting your time if this is stuff you already knew, I just wanted to put it out there.
    •  
      CommentAuthorjediknil
    • CommentTimeFeb 4th 2008
     
    I have to disagree with iluvcapra. Carbon is effectively deprecated, what with it not being ported to 64-bit. I wouldn't be surprised if Apple stops updating it completely in the future. XML nibs are also still serialized (keyed) object graphs and nearly impossible to even edit by hand (I've tried), much less create.

    Unless you can point to a reason why you shouldn't be using nib files, you should. That said, there are ways to get around it...for applications that /don't/ use/need a normal UI. The way Cocoa programmers get over the startup time/memory issue, BTW, is to have seperate nib files for UI elements that aren't loaded at the same time. (Don't go overboard with this, though; loading a second file takes time too, of course, and a bit of extra memory as well.)
    • CommentAuthoriluvcapra
    • CommentTimeFeb 4th 2008
     
    There is this meme among Mac developers that Carbon is deprecated, but I will not concede it, for the very good reason that Carbon _isn't_deprecated, and Apple has made no representations about deprecating or discontinuing Carbon now or in the future. Propagators of the "Carbon is deprecated" meme are haunted by this, and thus are compelled to use phrases like "effectively deprecated," with all respect to jediknil. Apple did not update Carbon to 64-bit, it's true, but Carbon and Cocoa apps can run side-by-side, on all OSX platforms, without emulation or degredation of user experience. Carbon is fully documented and supported by all the latest development tools from Apple.

    Various modules of Carbon have been deprecated, but I would note this is almost always done when there is a procedural C library replacing it. I'm very doubtful that Apple would deprecate the Carbon HI as long as they don't have a C API to replace it. As well, many cross-platform apps rely on Carbon: Photoshop, Microsoft Office, and my personal bete noir, Pro Tools, both sit on Carbon.

    But of course, this post, like the post preceding it, are speculation. YMMV. I personally will probably never write a Carbon app, but it's really more of a taste thing. OTOH, I'm just starting to discover Java programming, and I'm wishing Apple hadn't deprecated the Cocoa-Java bridge before I'd even had a chance to enjoy it, so you see why Apple sometimes has a bad rep with some developers.
    • CommentAuthoriluvcapra
    • CommentTimeFeb 13th 2008
     
    This vaguely applies, but it appears all iPhone apps will require building the GUI completely from code:

    http://furbo.org/2008/02/11/so-youre-going-to-write-an-iphone-app/

    And you write the iPhone apps in ObjC. This should be eye-opening.
  3.  
    This guy has a point though. I'm in the same mess.
    I recently made a basic calendar in Java. I successfully ported the model portion of it to C++ in an effort to learn the language only to realize how many hoops I need to jump through to get any graphical success. I got the console printing stuff out so I know its working. Time for graphics. XCode to the rescue. Or so i thought. Besides the HUGE syntax difference, I now must use IB.
    Now the calendar is made on the fly with the front and back arrows. Each month is different.
    Some have more rows of weeks than others depending on what day of the week the 1st falls on and how many days are in that month.
    But if I design it in Obj-c, it will be FIXED.
    So how do I do this? how do I get my calendar program to draw only the days I need?
  4.  
    > but it appears all iPhone apps will require building the GUI completely from code

    This is totally out of date. IB3 has supported iPhone GUI development for some time now.
    • CommentAuthormikeash
    • CommentTimeJan 1st 2009
     

    dreadrocksean, absolutely nothing prevents you from building a GUI in code just like you would in any other language. All of the GUI elements can be created and displayed using pure code (how do you think IB does it?) and this is not a problem in any way.