Why I can't release TTURLJSONResponse in UIViewController, but I can when I do it using TTURLRequestModel? - three20

Why I can't release TTURLJSONResponse in UIViewController, but I can when I do it using TTURLRequestModel?
When I use TTURLJSONResponse, it gives me EXC_BAD_ACCESS. Any idea why? is it gotta do with
[super requestDidFinishLoad:request];
Because in UIVIewController, it doesn't have that.
Cheers,
Mickey

It's hard to say w/o more code, but I'd say this isn't specific to Three20 or ViewController vs Model, but rather just the fact that you're trying to access something that you've de-alloced.
Have you tried using Three20's TT_RELEASE_SAFELY() method?
Or tracing your reference counting? (eg making sure you're retaining anything passed in to your class if you want to make sure it stays around)

Related

macOS Printing in Swift

Please note this is not an iOS question.
I have an NSView-based app (i.e. not document-based), and I’d like to bolt on a printing subsystem. I can get NSViews in my main controller to print ok. However, I want to have a special view constructed just for printing. The view should not show in the app’s window. The view contains two NSTextFields, two NSTextViews, and 5 labels.
I cannot seem to figure out a way to do this. I have tried various forms of these examples:
Add an NSView to my main view window? Seems logical, but it’s awkward in a storyboard, (I can’t position the view in the storyboard).
Programmatically create a custom NSView with a xib?
For this, I’ve tried:
#IBOutlet weak var printView: NSView!
….
let printOperation = NSPrintOperation(view: printView!)
This results in the comprehensive "fatal error: unexpectedly found nil while unwrapping an Optional value” message.
The outlets are configured correctly (I think)
A seperate ViewController? If so, how can I avoid having two print buttons — one to call the print controller, and the second, to print the PrintController’s view.
I’ve tried reading the Apple docs, but they are not the way I learn best. There are also no Print documents in Swift that I've found. I’ve waded through many SE questions, but have come up blank. Could you point me towards a solution please.
I think the specific problem here is that you're never causing the view to be loaded.
You can double check whether this is the case by overriding the viewDidLoad method on the controller. If it's never called, your view is never loaded from the nib.
Normally the UI machinery takes care of all that when you display a view controller, which is why it can be so confusing when it doesn't happen.
You should be able to trigger the view load by accessing the view property on the controller. e.g.
_ = self.view // Touch the view to force it to load
https://developer.apple.com/documentation/appkit/nsviewcontroller/1434401-view has some additional information.
You can also call loadView() directly although that's usually frowned upon.

Problems implementing AdMob because of Spritekit

After following the instructions from AdMob on how to implement their code, I faced the following problem.
I had to link the UIBannerView that was set up in the storyboard with my GameScene, this, as you might know, is not possible. So I decided to link it with my GameViewController. The problem here is that I can't decide when to show the BannerView since the value's and functions that decide this are implemented in the GameScene file. Is there any way I can fix this?
Thanks for your help,
Max Savelkoul
Move the methods from game scene into the view controller and call them by using NSNotificationCenter or delegation.
You could also add the banner code manually instead of using storyboard.
var adMobBannerAdView: GADBannerView!
and than set it up
adMobBannerAdView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
adMobBannerAdView.center = CGPoint...
Alternatively I have a helper on GitHub you could check out. Also helps to keep your code clean.
https://github.com/crashoverride777/Swift2-iAds-AdMob-CustomAds-Helper

How to manage windows in cocoa

I know the question is a bit generic but I guess my issue is generic as well.
I'm developing a small application in my free time and I decided to do it with Cocoa. It's nice, many things works almost automagically, but sometimes it's quite hard to understand how the framework works.
Lately I'm facing a new problem. I want to manage all the windows of the application from a single class, a front controller basically. I have a main menu and an "Import data" function. When I click it I want to show another window containing a table and call a method for updating the data. The problem is that this method is inside the class that implements the NSTableViewDataSource protocol.
How can I have a reference to that class? And more important, which should be the right way to do it? Should I extend the NSWindow class so that I can receive an Instance of NSWindow that can control the window containing the table (and then call the method)?
I may find several ways to overcome this issue, but I'd like to know which one is the best practice to use with cocoa.
PS: I know that there are tons of documentations files, but I need 2 lives to do everything I'd like to, so I thought I may use some help asking here :)
The problem is that this method is inside the class that implements the NSTableViewDataSource protocol.
How can I have a reference to that class?
These two sentences don't make sense, but I think I understand what you're getting at.
Instead of subclassing NSWindow, put your import window's controlling logic – including your NSTableViewDataSource methods – into a controller class. If the controller corresponds to a window, you can subclass NSWindowController, though you don't have to.
You can implement -importData: as an IBAction in your application delegate, then connect the menu item's selector to importData: on First Responder. That method should instantiate the import window controller and load the window from a nib.
In your import window controller's -awakeFromNib or -windowDidLoad method, call the method which updates the data.
Added:
Here's the pattern I'd suggest using in your app delegate:
#property (retain) ImportWindowController *importWC;
- (IBAction) showImportWindow:(id) sender {
if (!self.importWC)
self.importWC =
[[ImportWindowController alloc] initWithWindowNibName:#"ImportWindow"];
[self.importWC refreshData];
[self.importWC.window makeKeyAndOrderFront:sender];
}

- (void)swipeWithEvent:(NSEvent *)event not working on Lion?

I'm writing a simple cocoa program that should use the swipe gesture.
I've implemented in my NSView subclass the method swipeWithEvent: but when i try the program the method is never called. rotateWithEvent: method works instead.
I'm using a Xcode 4.1 on Mac OS 10.7 Lion.
Is there a difference between rotateWithEvent: and swipeWithEvent: ?? Why the first is called when I'm under the view and do a rotate gesture and the second in the same condition is never called if i do the swipe gesture?
Update :
I built also a simple project only to check the swipeWithEvent: and rotateWithEvent: methods but the behavior is the same.
Take a look at this sample code I wrote https://github.com/oscardelben/CocoaNavigationGestures
I think it would be helpful if you posted your code, reduced down to the bare essentials if possible.
One thing to look at is to make sure the method signature exactly matches the definition. In this case it should be:
- (void) swipeWithEvent: (NSEvent*) event
{
NSLog( #"A swipe happened" );
}
Make sure your definition matches this. Since you have a rotateWithEvent: that is working correctly this is probably unlikely but sometimes a typo can creep in.
Another thing you can do is to make a sample project that does nothing but respond to a swipe by logging (or whatever). This can help identify if there is something else ii your code or view hierarchy that is getting in the way.

resetting the image in an NSView

I think this is a very simple question, but I’m new to programming so I may be going about it in a wrong-headed way.
I have a basic understanding of Objective-C writing terminal applications and am teaching myself how to use the Cocoa GUI.
I understand how to use IBOutlet and IBAction to connect a simple button to a method that will repeatedly send random numbers to a textfield.
I understand how to add a NSView file, connect it to a custom view in interface builder and draw a path through random points in the view when the application launches.
(I’ve been putting this code inside the - (void)drawRect:(NSRect)dirtyRect method that is declared when the file is created).
What I can’t seem to figure out is how to connect a button to an action that will then ‘refresh’ the view – in this case repopulate it with another set of random points connected with a path. Looking at the documentation, I think I should somehow be using
– (void) setNeedsDisplay(BOOL)flag
but nothing I have tried so far had worked. Please tell me, what am I missing here?
Something like this:
- (IBAction)refreshButtonAction:(id)sender
{
[theView setNeedsDisplay:YES];
}
Connect your button to that action. "theView" is a reference to your custom NSView.

Resources