App crashes when I add a Activity indicator - iphone

I'm writing an app and ind the app I got a Web View and I want to add an Activity Indicator. So I use this code:
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0)
target:self selector:#selector(loadingPage) userInfo:nil repeats:YES];
But when I insert this code my app crashes and refers me to this code:
main.m
#import <UIKit/UIKit.h>
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
at the line that starts with "int retVal..." it tells me "Thread 1: Program recieved signal: "SIGABRT"". By the way this code that it shows me is not something I wrote it was in the Xcode project from the beginning.
So my question is: Does anyone know a simple way of fixing this or another simple way of adding a Activity Indicator?
If someone knows a better way pleace write the code since I'm new to app development and still have allot to learn.
In advance thank you very much :)
Here's some more code:
.h file
#interface Ugeblad : UIViewController {
// Web View
IBOutlet UIWebView *faktaPDF;
// Activity Indicator View
IBOutlet UIActivityIndicatorView *pageLoadingIndicator;
NSTimer *timer;
}
#property (nonatomic, retain) IBOutlet UIWebView *faktaPDF;
#property (nonatomic, retain) IBOutlet UIActivityIndicatorView *pageLoadingIndicator;
#end
.m file
[faktaPDF addSubview:pageLoadingIndicator];
[faktaPDF loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"Website"]]];
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:#selector(loadingPage) userInfo:nil repeats:YES];

If by activity indicator you mean a spinning wheel (known as the UIActivityIndicatorView), you don't need a timer for it. It animates on its own; you just have to place it on the view and call [startAnimating].

Don't use an NSTimer for this, that's gross. Assign yourself as the delegate of the UIWebView and implement
-webViewDidStartLoad: to present your "loadingView" and then implement
-webView:didFinishLoad: and -webView:didFailLoadWithError: to hide your "loadingView".

Related

no known class method for selector 'scheduledTimerWithTimeInterval with timer

I have tried to figure out this, by reading other threads here - but I have not been able to work out anything so far.
If it's be just being a novice that doesn't understand the connections I need to make or if I doesn't have any relevance - I don't know.
I am making an iPhone app with 2 timers in it.
I am working on getting the source code for the timer to work.
I use a flip side application template from xcode, as I want to use the flip side for settings later on (where you can customize timers
In my MainViewController.h:
#import "FlipsideViewController.h"
#import "UIKit/UIKit.h"
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate, UIPopoverControllerDelegate> {
IBOutlet UILabel *timerP1;
IBOutlet UILabel *timerP2;
NSTimer *myTimerP1;
NSTimer *myTimerP2;
}
- (IBAction)startP1;
- (IBAction)stopP1;
- (IBAction)resetP1;
- (void)showActivityP1;
- (IBAction)startP2;
- (IBAction)stopP2;
- (IBAction)resetP2;
- (void)showActivityP2;
#property (strong, nonatomic) UIPopoverController *flipsidePopoverController;
#end
In my MainViewController.m:
#interface MainViewController ()
#end
#implementation MainViewController
- (IBAction)startP1{
myTimerP1 = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(showActivityP1) userinfo:nil repeats:YES];
}
- (IBAction)stopP1{
[myTimerP1 invalidate];
}
- (IBAction)resetP1{
timerP1.text = #"60";
}
- (void)showActivityP1;{
int currentTimeP1 = [timerP1.text intValue];
int newTimeP1 = currentTimeP1 - 1;
timerP1.text = [NSString stringWithFormat:#"%d", newTimeP1];
}
- (IBAction)startP2{
}
- (IBAction)stopP2{
}
- (IBAction)resetP2{
}
- (void)showActivityP2{
}
I get the error "No known class method for selector 'scheduledTimerWithTimeInterval:target:selector:userinfo:repeats:'
What am I doing wrong here?
What am I doing wrong here?
Well, frankly what you're doing wrong is not using autocompletion.
It's a small typo, nothing to worry about.
userinfo should be userInfo (note the capital I)
So the complete call becomes:
[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(showActivityP1)
userInfo:nil
repeats:YES];
Oh and by the way - (void)showActivityP1;{...} will cause you troubles as well, just drop the ;

NSTimer change image iPhone Programming

How to change images periodically using NSTimer in iPhone progranmming?
I create an image view for loading images.
I want to display images in imageview and periodically change images by using NSTimer.
Instead of using NSTimer, I would just use an arrary of UIImages and animate them. See the documentation from here.
import the images you like to your bundle application
called them image1.png, image2.png image3.png and so on....
Add UIImageview in interface builder call it (reference) "theImage"
in the H file:
#interface delme3ViewController : UIViewController {
UIImageView *theImage;
int imageCount;
}
#property (nonatomic, retain) IBOutlet UIImageView *theImage;
#end
in the M file:
- (void)viewDidLoad
{
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:#selector(animateFunction)
userInfo:nil
repeats:YES];
imageCount = 1;
}
-(void)animateFunction
{
NSLog(#"Timer heartbeat %i",imageCount);
imageCount = imageCount + 1;
NSString *imageName = [NSString stringWithFormat:#"image%i.png"];
[theImage setImage:[UIImage imageNamed:imageName]];
}
you can also use array of image names rather renaming images in your bundle. :)

Stuck with IBOutlet file

So, I am following this Xcode tutorial book: Iphone and Ipad Game Development for Dummies. Its a good book, but there are some flaws here and there. Most of the time I can figure a way around these flaws, but now I stumbled upon something I cant fix. Even their website or contacts dont know the answer.
Im trying to make a game with cars and stuff. But the problem lies with the main menu. I already made buttons with custom button mode. And used IbActions to make them work(actually, "New Game" is the only button that works now cause it has its function already scripted). But now I have to make IBOutlets to give them an Animation. The book told me to use the QuartzCore.framework. Now the problem is that when I try to build it I get this error: No Declaration of property 'newGameButton' found in the Interface.
This is my script.
The header file:
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#interface MainMenuViewController : UIViewController {
//This has changed
#property(nonatomic,retain) IBOutlet UIButton* newGameButton;
#property(nonatomic,retain) IBOutlet UIButton* statsButton;
#property(nonatomic,retain) IBOutlet UIButton* settingsButton;
CAKeyframeAnimation* popAnimation;
}
-(IBAction) newGame:(id)sender;
-(IBAction) showStats:(id)sender;
-(IBAction) showSettings:(id)sender;
#end
And the .m file (its called implantation right?):
#import "MainMenuViewController.h"
#import "TrafficAppDelegate.h"
#import "TrafficViewController.h"
#implementation MainMenuViewController
-(IBAction) newGame:(id)sender{
TrafficViewController* traffic = [[TrafficViewController alloc] initWithNibName:#"TrafficViewController" bundle:nil];
[self.navigationController pushViewController:traffic animated:NO];
}
-(IBAction) showStats:(id)sender{
}
-(IBAction) showSettings:(id)sender{
}
#synthesize newGameButton, statsButton, settingsButton;
-(void)viewDidLoad{
[super viewDidLoad];
popAnimation = [CAKeyframeAnimation animationWithKeyPath:#"transform.scale"];
popAnimation.keyTimes = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:0.7], [NSNumber numberWithFloat:1.0], nil];
popAnimation.values = [NSArray arrayWithObjects: [NSNumber numberWithFloat:0.01], [NSNumber numberWithFloat:1.1], [NSNumber numberWithFloat:1.0], nil];
[popAnimation retain];
}
-(void)popView:(UIView*)view{
[view setHidden:NO];
[[view layer] addAnimation:popAnimation forKey:#"transform.scale"];
}
-(void)viewWillAppear:(BOOL)animated{
[popAnimation setDuration:0.3];
[newGameButton setHidden:YES];
[statsButton setHidden:YES];
[settingsButton setHidden:YES];
[self performSelector:#selector(popView:) withObject:newGameButton afterDelay:0.25];
[self performSelector:#selector(popView:) withObject:statsButton afterDelay:0.3];
[self performSelector:#selector(popView:) withObject:settingsButton afterDelay:0.35];
}
#end
Now I first tought that they werent connected to the buttons in the User Interface builder. But that didnt work either. I tried to remake the whole script. I tried to reload the script in Xcode. I even tried to make my own version. Im dont have any options left. Does anyone know what is wrong with my script?
set the property of the objects in header file as
#property(nonatomic, retain) IBOutlet UIButton* newGameButton;
.
.
.
and in .m file synthesis them using
#synthesis newGameButton;
do this for all the objects that you have declared
and then connect them in the IB
Are you loading the proper UIViewController?? i mean, if you are working with the interface builder check out that in your main window xib, the viewController object you are using corresponds with your MainMenuViewController class
You have said you were seeing:
"No Declaration of property 'newGameButton' found in the Interface"
Any property that is declared in either the .h (header) or .m (implementation) must be synthesised:
#property (nonatomic,retain) IBOutlet UIButton* newGameButton;
Like this for your properties in the .m (implementation)
#synthesize newGameButton;

Possibilities to use NSTimer global

I am implementing an iPhone application. In that I need to use a timer to call a method to get the values frequently. And Also I need to stop the Timer in another method. So I need to use it as global. But I don't an idea to create a global Timer can you guys please help on this.
Thanks in Advance,
Sekhar Bethalam.
Just keep a reference to the timer.
In .h file:
NSTimer *timer;
..
#property (nonatomic, retain) NSTimer *timer;
In .m file:
#synthesize timer;
..
// Initiation:
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(timerFireMethod:) userInfo:nil repeats:YES];
..
// Termination:
[self.timer invalidate];
In the interface, declare a property:
#interface myViewController : UIViewController
{
NSTimer *timer;
}
#property (nonatomic, retain) NSTimer *timer;
#end
and then synthesise it in the .m file:
#implementation myViewController
#synthesize timer;
and then whenever you need to use it, just use timer.

iPhone UIBarButtonItem setEnabled in NSThread

I'm trying to change the enabled property of an UIBarButtonItem after doing some stuff in an NSThread. After pressing the button I set the enabled to NO then perform the threaded part and at the end I try to re enable the button. Fairly basic.
Somehow this fails, however I'm able to change any other property of the the UIBarButtonItem correctly (for ex. title).
What am I doing wrong here?
#interface myViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
IBOutlet UIBarButtonItem *myButton;
}
#property (nonatomic, retain) IBOutlet UIBarButtonItem *myButton;
- (IBAction)mysub:(id)sender;
#end
#implementation myViewController
#synthesize myButton;
- (IBAction)mysub:(id)sender {
[myButton setEnabled:NO];
[NSThread detachNewThreadSelector:#selector(mysub_threaded) toTarget:self withObject:nil];
}
- (void) mysub_threaded {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
… do threaded stuff
[myButton performSelectorInBackground: # selector(setEnabled :) withObject: [NSNumber numberWithBool:YES]];
[pool drain];
}
You want performSelectorOnMainThread instead.
[myButton performSelectorOnMainThread:#selector(setEnabled:)
withObject:[NSNumber numberWithBool:YES]
waitUntilDone:NO];
Always do anything that touches the UI on the main thread.
But sometimes passing arguments like this is funky too. I find it best to wrap up everything you need done in another method
- (void)mysub_complete {
[myButton setEnabled:YES];
}
Then call that with
[self performSelectorOnMainThread:#selector(mysub_complete)
withObject:nil
waitUntilDone:NO];
Now you can do as much other UI stuff as you want without worry about it.