Managing multiple UIActivityIndicatorView - iphone

I have defined IBOutletCollection of UIActivityIndicatorView.
IBOutletCollection(UIActivityIndicatorView) NSArray *ticker;
After making the required connections in the Interface builder, I run the following code.
for (UIActivityIndicatorView *obj in ticker)
{
[obj startAnimating];
}
But I get this message on the console:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIActivityIndicatorView countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x2c86a0'
I know it might be a very small problem with it, but really not getting it.
Thanks!

I don't know what it's not working for you. I have done the following and it works fine.
in my .h
#property (strong, nonatomic)IBOutletCollection(UIActivityIndicatorView) NSArray *activityIndicators;
in my .m (if not using Xcode 4.4)
#synthesize activityIndicators
I added 7+ activity indicators to my StoryBoard and connected them to my viewController
in my viewDidLoad i'm calling
for (UIActivityIndicatorView *obj in self.activityIndicators){
[obj startAnimating];
}
Works fine for me in this configuration no problems.
Based on the error message you're getting
'NSInvalidArgumentException', reason: '-[UIActivityIndicatorView countByEnumeratingWithState:objects:count:]
Sounds like something not set right, check your IB that you haven't done something odd.

The method countByEnumeratingWithState:objects:count: is supposed to be sent to the array object as part of the NSFastEnumeration protocol. Sometimes messages can get sent to the wrong object if there's a memory issue. Double-check to make sure that ticker is being properly retained (and it might also be best practice to use self.ticker).

Related

Xcode: Button clicked results in NSInvalidArgumentException

I'm working in Xcode 4.3.2.
I am new to Xcode. I'm building an application that must change to different views on button clicks. My files are: AppDelegate.h/.m, GreenViewController.h/.m, SwitchViewController.h/.m, GreenView.xib - I am not using storyboards, but my project demands that I don't use them (backwards compatibility issues).
Here's my problem (it seems very simple): I'm trying to print to the console when a UIButton (placed in GreenView.xib) is clicked. Here's my code for GreenViewController.h
#import <UIKit/UIKit.h>
#interface GreenViewController : UIViewController
- (IBAction)switchViews:(id)sender;
#end
Here's my (deprecated) code for GreenViewController.m:
#import "GreenViewController.h"
#implementation GreenViewController
- (IBAction) switchViews:(id)sender {
NSLog(#"Button Pressed!");
}
The owner for GreenView.xib is GreenViewController.
For some reason I have the error only when the UIButton (in GreenView.xib) is pressed:
2012-10-09 18:07:38.490 MyViewSwitcher[8655:f803] -[SwitchViewController switchViews:]: unrecognized selector sent to instance 0x688a660
2012-10-09 18:07:38.492 MyViewSwitcher[8655:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SwitchViewController switchViews:]: unrecognized selector sent to instance 0x688a660'
It seems that SwitchViewController is expecting something from the method "switchViews", but "switchViews" is only listed in GreenViewController. Before, I had "switchViews" in SwitchViewController, but I deleted all the code corresponding to the method & all the connections. Again, I've double checked that "switchViews" in GreenViewController is connected to the UIButton found in GreenView.xib. I've already cleaned & re-built my project & I still get this error.
Thanks for your help!
What your error is saying is that you are calling the switchViews: method on an instance of SwitchViewController. And since there is no definition for switchViews: (because you deleted it) for the class SwitchViewController, it doesn't know what to do, and crashes.
Hate to tell ya this, but your button is connected to the switchViews: method of a SwitchViewController. You say "I've double checked that 'switchViews' in GreenViewController is connected to the UIButton found in GreenView.xib". Well, yea, from your crash, it is. But are you sure its connected to the switchViews: function of a GreenViewController instance? How did you check this information?
What I would suggest is deleting all connections to the UIButton in the connection inspector. Then reconnecting it to the view controller (which you say is a GreenViewController). It should then bring up the list of IBActions which should only be the switchViews: method.
If you do that, and it still doesn't work. Try deleting the button and remaking it, then reconnecting it.

iPhone app crashes with admob?

I am having an iphone app with admob on two screens's viewdidLoad
My code is:
AbMob =[[GADBannerView alloc]initWithFrame:CGRectMake(0.0,self.view.frame.size.height-195, 320, 50)];
AbMob.adUnitID = AdMob_ID;
AbMob.rootViewController = self;
[self.view addSubview:AbMob];
GADRequest *r = [[GADRequest alloc] init];
r.testing = NO;
[AbMob loadRequest:r];
Problem is this code works fine on one screen but crashes on other screen with error
* -[GADOpener didOpen]: message sent to deallocated instance
0x6074750
Can anybody tell me what could be the problem
You have a retain/release problem somewhere in your code. You say it works in one view, but not another - this makes me believe that you are storing this instance outside of your view controllers. The message sent to deallocated instance issue is due to you trying to use a variable that has been removed from memory somewhere in the code before this error pops up. You need to ensure that the view controller that is creating this object is properly retaining it so that it does not get deallocated before you need to use it again with:
GADBannerView *_adMobBannerView;
#property(nonatomic,retain) GADBannerView *adMobBannerView; //view controller retains object when using self.adMobBannerView = bla
It sounds like you may need to brush up on your memory management documentation, but the gist of it is that anywhere you are calling alloc, you are managing that memory and need to call release when you are done with it. If you need a variable to stick around for longer than an autoreleased object is living for, then you need to create an instance variable and retain the object yourself with ivar properties #property (nonatomic, retain).

Crash (SIGABRT) while adding a view. (WebView)

I am having a crash (triggered by an exception) while adding a WebView. I haven't figured out why. And yes I have browsed the web because this is a very common problem though with no success, I found an answer saying that I should implement the DSYM to track the stack correctly, because looking at all those addresses is just meaningless (and I agree), the thing is I have no idea on how to do this. I found this: save dsym files. But I didn't figure out how to integrate it into my project, besides it looks dangerous. And yes I have NSZombie enabled too, but to no avail.
Anyway now to the point of my question. I have a button that triggers an event, the event changes a string (the URL) according to the button pressed. Then this IBAction calls on the delegate, and the delegate makes a transition to a view that has a UIWebView that will open with the URL edited by the IBAction. So I have the following code:
This is the Delegate method:
WebViewController *awebViewController = [[WebViewController alloc]
initWithNibName:#"WebView" bundle:nil];
[self setWebViewController:awebViewController];
[awebViewController release];
self.webViewController.finalURL = [NSMutableString stringWithFormat:#"%#", linkString];
[viewController.view removeFromSuperview];
// HERE happens the crash, found out using breakpoints
[self.window addSubview:[webViewController view]];
[UIView commitAnimations];
[viewController release];
viewController = nil;
This is the exact crash message:
-[WebView initWithCoder:]: unrecognized selector sent to instance 0x5718320
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '[WebView initWithCoder:]: unrecognized selector sent to instance 0x5718320'
I have the theory that it tries to make the transition but that there is no actual view to add. Though that is a wild guess. Exceptions are usually called by those kinds of things but in this case I don't see what is happening.
Thank you for your help, and forgive me if it is too dumb a question.
a guess from my side:
Do you have a view in your nib that should be a UIWebView but you changed the Class of that view to WebView?
You should first check this in interface builder.
This exception is exactly what happens if you change the Class of a view element to a class that isn't available.
The exact error is that some instance of a class called WebView (not UIWebView) at memory address 0x5718320 is being sent the message -initWithCoder:, which it doesn’t recognize. That message is called when loading views created with Interface Builder; check your nibs for a view that you’ve changed to a custom WebView class.
I had same problem, and renamed class WebView into WebViewCustom by XCode refractor/Rename feature, cleaned up project and now it works fine.

Works in iPhone Simulator, but not on Phone

So I have an app which runs fine on the simulator, but not on the actual device.
At runtime, it gives me the following error:
2010-12-05 19:58:32.006 Sports[4668:307] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableView isEqualToString:]: unrecognized selector sent to instance 0x800800'
A bit about the structure: I have a Tab Bar Controller, the first view being a UINavigationController.
The view in there is a UITableView. One thing that may or may not be related is that if I do #synthesize tableView; in the table view controller, the table stays blank on both simulator and phone (but does not crash).
If I take that out, it loads the correct data on the simulator, and crashes on the phone.
Where should delegate/dataSource be linked to in the Interface Builder? I tried linking it to "View" to "File's Owner", and making a new "ViewController" and none of those worked.
Both the delegate and dataSource should be linked to File's Owner, which is the view controller class that declares the table view as an IBOutlet; this should be the same view controller that owns the nib file. Additionally, that view controller should be implement the UITableViewDelegate and UITableViewDataSource protocols.
Add that #synthesize line back in, (unless you're subclassing UITableViewController, as pointed out by grahamparks in the comments!) make sure those connections are right, and, finally, make sure you've declared an IBOutlet for the table view, and connected that properly between your class and interface builder.
Found it!
Turns out that there wasn't really a problem with this at all. The problem was that the date field in my database, when run on my phone was always zero/nil.
Why? Because the NSDate object created never initialized and stayed at nil.
Why?
Because my phone is in 24 hour time and did not parse the am and pm properly.
Lessons learned!
Run your app with NSZombieEnabled set to yes. See http://www.cocoadev.com/index.pl?NSZombieEnabled for an explanation about it and how to set it. I think what you will find is that the app will now throw an exception in the simulator telling you that you are sending a message to an object that has been released.
Based on the error message you are receiving, I expect the culprit is a NSString or NSMutableString object. If it is an NSString then warning, that NSString could be shared by several different objects so figuring out where the extra release is might be hard.
Never fear though, Instruments helps tremendously in this regard. Here is a link that explains how to use Instruments to find out exactly where your object is being retained and released so you can track down which release is inappropriate. http://www.markj.net/iphone-memory-debug-nszombie/
Good luck!

What is the difference between setting the delegate in Interface Builder vs using setDelegate:?

I'm trying to set the delegate for a scroll view using Interface Builder.
If I have code like this:
MyScrollViewDelegate * delegate = [[MyScrollViewDelegate alloc] init];
[scrollView setDelegate:delegate];
in viewDidLoad, everything works perfectly.
If I open Interface Builder, add an NSObject and set the class to MyScrollViewDelegate, then link the scrollView's delegate to my instance of MyScrollViewDelegate and inspect [scrollView delegate] inside viewDidLoad, it looks like an instance of MyScrollViewDelegate, and I can interact with it, set proprerties etc, looks good.
However, when I scroll inside the scroll view I get an NSInvalidArgumentException:
*** -[NSCFArray scrollViewDidScroll:]: unrecognized selector sent to instance 0x3d319a0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFArray scrollViewDidScroll:]: unrecognized selector sent to instance 0x3d319a0'
So, questions:
What is the difference between setting the delegate in Interface Builder vs using setDelegate:?
Why is "[NSCFArray scrollViewDidScroll:]" in the console, not, [MyScrollViewDelegate scrollViewDidScroll:]?
What could I be doing wrong?
There is no difference in setDelegate: itself.
However, you have a memory management issue. The problem is that objects don't retain their delegates (to avoid reference cycles). Your delegate is getting deallocated, and the memory is reused for an array. This is why you see a message intended for your delegate getting dispatched to an array.
You are seeing a difference because of an oddity of Interface Builder memory management. The "top-level objects", those objects that appear alongside File's Owner and Main Menu at the top level in the IB doc window, have an unbalanced retain on them. That's keeping your delegate alive in the IB case.
The solution is for something, perhaps your app delegate, to retain the scroll view delegate.
See Memory Management of Nib Objects.