Help Porting Some iOS Code

Discussion in 'Mac Programming' started by Blakeasd, Jul 3, 2011.

  1. macrumors 6502a

    Joined:
    Dec 29, 2009
    #1
    Hello,
    I have been following along with this tutorial
    and I need some help porting some code to OS X.
    1. Is viewDidLoad: the equivalent to awakeFromNib: ?
    2. How did the author create those shapes and how can I do this in OS X?

    I am only having a problem with the viewDidLoad: stuff.
    Thanks!
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    viewDidLoad: is part of UIViewController while awakeFromNib can be called on any object created from a nib. However viewDidLoad: can also be called if the view was created programatically. So awakeFromNib can probably be used in most cases on OS X.
     
  3. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #3
    They're UIViews with the background color set to different values.

    The Mac OS X equivalent (NSView) doesn't have a background color.

    NSTextField does have a background colour. So you could drag Wrapping Labels onto your window instead and just delete the text in the title.
     
  4. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #4
    So for the viewdidLoad: iOS method I create a NSViewController and call
    -(void)awakeFromNib: and in that awakeFromNib: method I can use the same iOS code that is in the viewDidLoad? Do I create a different NSView subclass for each of the colored squares and in their awakeFromNib: method I call addPhysicalBodyForView: ?
     
  5. macrumors 603

    Joined:
    Aug 9, 2009
    #5
    Or maybe NSBox or NSColorWell.

    See AppKit Framework Reference.
     
  6. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #6
    I know about the background color, but how exactly do I handle the views I create? I have no idea how to convert this code:
    Code:
    - (void)viewDidLoad
    {
    	[super viewDidLoad];
     
    	[self createPhysicsWorld];
     
    	for (UIView *oneView in self.view.subviews)
    	{
    		[self addPhysicalBodyForView:oneView];
    	}
     
    	tickTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
    }
     
  7. jiminaus, Jul 4, 2011
    Last edited: Jul 4, 2011

    macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #7
    Just create them in Interface Builder. Porting iOS viewdidLoad: to awakeFromNib: is probably good enough for this tutorial. No need to create view controllers. This is a simple tutorial. Just do it in the app delegate.


    Start by changing UIView to NSView.
     
  8. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #8
    Thanks, I think I got it working. Here is my new code:
    Code:
    -(void)awakeFromNib{
    
    
        [self createPhysicsWorld];
        NSView *contentView;
        contentView = [window contentView];
        for (NSView * oneView in [contentView subviews]){
        
        
            [self addPhysicalBodyForView:oneView];
        
        }
            
     
        
        
        
        }
    
    
    I have one more question, I get an error with this line of code:
    Code:
    physicalView.tag = (int)body;
    
    The author said that this line would "abuse so the tag property", so what does this line of code do and what can I use in place of it? This line compiles in iOS, but not in Mac OS X. I get the error, " Cast from pointer to small type 'int' loses information "
    Thanks!
     
  9. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #9
    Firstly the author was ignorant in type-casting a pointer to an int. Type-cast it to an NSInteger.

    Secondly, while NSView has tag, it doesn't have setTag:. NSControl though does have setTag:. All of NSTextField, NSBox and NSColorWell inherit from NSControl, so you should be okay. But you'll need to change addPhysicalBodyForView: to accept an NSControl* instead of an NSView*. You'll then need to guard the call to addPhysicalBodyForView: in awakeFromNib: so it only happens with subviews that are kinds of NSControl.
     
  10. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #10
    Okay, Thanks!
    I did everything but this:
    I am not exactly sure what guarding a call is.
     
  11. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #11
    Ah sorry. Guarding might be an Ada-ism. Surround the call with an if statement that tests that the view is kind of NSControl.
     
  12. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #12
    I'm still not quite sure what you mean.
    Thanks
     
  13. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #13
    Code:
        for (NSView * oneView in [contentView subviews]){
        
            [COLOR=blue]if ([oneView isKindOf:@class(NSControl)]) {[/COLOR]
        
                [self addPhysicalBodyForView:[COLOR=blue](NSControl*)[/COLOR]oneView];
    
            [COLOR=blue]}[/COLOR]
        
        }
    
     
  14. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #14
    The code you provided didn't compile and gave the following error:
    "Unexpected @ in program"
    I changed the code to this:
    Code:
       if ([oneView isKindOfClass:[NSControl class]]) {
                
                [self addPhysicalBodyForView:(NSControl*)oneView];
                
            }
    
    
    Is the code I created correct?
     
  15. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #15
    Sorry for getting the @class construct wrong. I am actually away from my beloved Mac. The code you posted looks correct.
     
  16. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #16
    Thanks for all your help! I can't wait to do some cool physics stuff.
    :D
     
  17. Blakeasd, Jul 5, 2011
    Last edited: Jul 5, 2011

    thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #17
    I actually do have another question,
    I will be applying a CGAffineTransform to a view (same tutorial) and the author is using an iOS only property
    Code:
    
    CGAffineTransform transform = CGAffineTransformMakeRotation(- b->GetAngle());
    oneView.transform = transform
    . What is an equivalent way to do this in OS X?
     
  18. macrumors 603

    Joined:
    Aug 9, 2009
    #18
    Look it up in the documentation: NSView Programming Guide.
     
  19. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #19
    I actually didn't find a method or property that is the equivalent to the iOS property.
     
  20. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #20
    To the best of my knowledge there isn't one. The guide you were directed to has a lot of detail on how to achieve a similar thing in OSX (hint it's not as simple as setting a property).
     
  21. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #21
    I can't find anything there about CGAffineTransforms on NSViews. :confused:
     
  22. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #22
    That's because there isn't such a thing. As it says you are better performing the rotation yourself on the co-ordinate system in drawRect:. If it's not a custom view you can use the obvious methods to set either bounds or frame rotations.
     
  23. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #23
    So I won't be able to use my CGAffineTransformation?
     
  24. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #24
    Here is my new code:
    Code:
     
    			CGAffineTransform transform = CGAffineTransformMakeRotation(- b->GetAngle());
                
                oneView.wantsLayer = YES;
                [oneView.layer setAffineTransform:transform];
    
    
    
    
    Can anyone confirm the code I wrote works?
    I tried to compile my project and I get no errors, but all the shapes don't fall to the bottom of the window like they should. Could someone PLEASE take a look at my project and find out why it isn't working?
    Here is the link to my project: Project Link
     

Share This Page