Range or index out of bounds (App is crashing) - xcode

I have an app with ads banner (inner-active) in every view controller (4 views) and i keep getting this error after 2-3-4 minutes that my app is running:
'NSRangeException', reason: '*** -[__NSCFString substringToIndex:]: Range or index out of bounds'
My app is crashing & i can't find the solution for that, but i know for sure that is something with my AD banner issue cau's when i comment the ad banner code everything works great. i can assume that it happens when new request is loading on the banner.
This is the code i'm using for the AD banner:
.h file:
#property (nonatomic, retain) UIView *adBanner;
.m file:
Did synthesize & import for what i need and after that:
- (void)viewWillAppear:(BOOL)animated
{
CGRect frame = CGRectMake(0, 430, 320, 50);
self.adBanner = [[UIView alloc] initWithFrame:frame];
[self.view addSubview:self.adBanner];
// Display ad
if (![InneractiveAd DisplayAd:#"iOS_Test" withType:IaAdType_Banner withRoot:adBanner withReload:60 withParams:optionalParams])
{
adBanner.hidden = YES;
}
}
This is my AppDelegate (don't know why but maybe it something with that too?):
.h file:
#import <UIKit/UIKit.h>
#class RootViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (nonatomic, strong) RootViewController *rootViewController;
#end
.m file:
#import "AppDelegate.h"
#import "RootViewController.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize rootViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.rootViewController = [[RootViewController alloc] initWithNibName:nil bundle:NULL];
[self.window addSubview:self.rootViewController.view];
[self.window makeKeyAndVisible];
return YES;
}
I realy don't understand what it could be and how it related to NSCFString :\
Thanks.

Not sure if you got an answer to this but I was getting it with this line of code when there was no internet connection.
NSString *value = [serverOutput substringWithRange:NSMakeRange(2, [serverOutput length] - 2 * 2)];
I had to protect it with a check to the internet connection and an if then statement

Related

Black sceen trying to put a .Xib after Splash

I'm programming a App for iOS 8. I've a problem because i don't want use storyboard, I want to put a .xib file at the first time. Muy problem is that always, after splash be a black screen.
The Class who I want to put after Splash is LoginViewController
At general options, the "Main interface" is white (empty) (if put the name of the class, i've NSException (NSUknownException)).
In the .xib file i'have the files owner connected, and at the right side of the screen i've the name of the class at "Custom Class" (LoginViewController).
My appDelegate.h is: (I try with " #property (strong, nonatomic) LoginViewController *viewController;" too)
#import <UIKit/UIKit.h>
#import "LoginViewController.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UIViewController *viewController;
#end
My appDelegate.m is: (i try a lot of variants)
#import "AppDelegate.h"
#import "LoginViewController.h"
#interface AppDelegate ()
#end
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.viewController = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
navController.navigationBarHidden = YES;
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
#end
The LoginViewController.h:
#import <UIKit/UIKit.h>
#interface LoginViewController : UIViewController
{
IBOutlet UILabel *labelPrueba;
IBOutlet UIButton *botonPrueba;
IBOutlet UIButton *boton2prueba;
IBOutlet UILabel *label2Prueba;
IBOutlet UILabel *dsfd;
IBOutlet UIButton *dfdf;
}
#end
The LoginViewController.m:
#import "LoginViewController.h"
#interface LoginViewController ()
#end
#implementation LoginViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
label2Prueba.text = #"laaaaaaaaaaaaa";
NSLog(#"Entra en viewDidLoad");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#end
Someone can help me please. I'm going crazyyyyy.
Thanks a lot.
The solution was put this windows like init frame:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// That was the solution
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// That was the solution
self.viewController = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
navController.navigationBarHidden = YES;
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
Thanks

How to import an UILabel from one UIViewcontroller to another

I've got a problem with xcode.
I'm a noob with object-c and xcode so... please help.
I have 2 Viewcontrollers: ViewController (with .m/.h) and HighScores (with .m/.h).
In HighScores I have put a label called first. And in ViewController I have a UITextField called *textField. I want the text in textField to be in the label when I enter the text AND when the score of the game already played is grater than the text already existing in the label ('first').
So,
This is how my HighScore.h looks like:
#import <UIKit/UIKit.h>
#interface HighScores: UIViewController {
IBOutlet UILabel *first;
}
#end
and this is ViewController.m:
#import "ViewController.h"
#import "HighScore.h"
...
NSString *myString = [HighScores.first];
if (score.text > myString) {
NSString *string = [textField text];
[HighScores.first setText:string]
but xcode says there's an error when I type "first" after the dot '.'... How can I make it if I want xCode to recognize the "first" label from HighScore UIViewController in VewController UiViewController?
Thanks!
In your code "first" is a UILabel , it will be generated when view of highScores get loaded .
Because it is a IBOUtlet.
Secondly you are trying yo access with class name .
Make a instance of HighScore class first and than try to acess label "first".
#import <UIKit/UIKit.h>
#interface HighScores: UIViewController
#property (nonatomic , strong)UILabel *firstLabel ;
#end
#implementation HighScores
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle
{
self.firstLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
[self.view addSubview self.firstlabel];
}
#end
than in ViewController.m
HighScore * highscoreObject = [[HighScore alloc]init];
NSString *mystring = [highscoreObject.firstLabel text];
if (score.text > mystring) {
[highscoreObject.firstLabel setText:score.text];
{
Lets use notification , if you are confused in here:
In this case you can also use IBoutlet.
We will throw an notification with the string to be set and read the notification in HighScores and set the label with the string send.
In ViewController.m
if (score.text > myString) {
NSString *string = [textField text];
[[NSNotificationCenter defaultCenter] postNotificationName:#"update" object:string];
}
#interface HighScores: UIViewController
#property (nonatomic , strong) IBOutlet UILabel *firstLabel ;
#end
and in HighScores.m
#implementation HighScores
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(changetext:) name:#"update" object:nil];
}
- (void) changetext:(NSNotification *)notification {
NSLog(#"Received");
self.firstLabel.text = [notification object];
}

blank screening after going back

I've recently created a app with Xcode. When 2 images collide, it sends me to a new screen (which is good). However, when i push the button to return me back to the original view, it goes blank. Can you tell me why? Any feed back is appreciated.
Here is code from the First .h file:
import
#interface ViewController : UIViewController {
IBOutlet UIImageView *image1;
IBOutlet UIImageView *image2;
}
#property (nonatomic, retain) UIImageView *image1;
#property (nonatomic, retain) UIImageView *image2;
-(void)collision;
#end
And from the .m:
import "ViewController.h"
import "newview.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize image1, image2;
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *mytouch = [[event allTouches] anyObject];
image1.center = [mytouch locationInView:self.view];
[self collision];
}
-(void)collision {
if (CGRectIntersectsRect(image1.frame, image2.frame)) {
newview *second = [[newview alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
}
}
here is code from the second .h (The one with the go back button):
(IBAction)retry:(id)sender;
and from the .m:
import "newview.h"
import "ViewController.h"
#interface newview ()
#end
#implementation newview
(IBAction)retry:(id)sender {
ViewController *second = [[ViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
}
The blank screen is another modal view from (IBAction)retry:(id)sender.
You should use [self dismissModalViewControllerAnimated:YES]; in (IBAction)retry:(id)sender for going back to previous screen instead of presenting another modal view.

Why won't this simple view switching project work?

I'm in the process of learning how to write apps in Xcode. I'm using a book to guide me. Unfortunately the book is written with guides to Xcode 3 and I'm using Xcode 4.
Now so far there haven't been any problems, but this project doesn't work, and I simply don't get it, because it seems to make pretty good sense.
The project's goal is to use a view controller to switch between three views.
Could anyone please take a look and see what I did wrong?
Here is the entire project: http://www.2shared.com/file/CKO6ACzg/MultipleViews.html
PS: I know that as it is now the views will be stacked on top off each other and that the view isn't being cleared when you click a new button.
MultipleViewsViewController.h should be:
#import <UIKit/UIKit.h>
#class FirstViewController;
#class SecondViewController;
#class ThirdViewController;
#interface MultipleViewsViewController : UIViewController {
IBOutlet FirstViewController *firstViewController;
IBOutlet SecondViewController *secondViewController;
IBOutlet ThirdViewController *thirdViewController;
}
//#property (nonatomic, retain) FirstViewController *firstViewController;
//#property (nonatomic, retain) SecondViewController *secondViewController;
//#property (nonatomic, retain) ThirdViewController *thirdViewController;
-(IBAction)loadFirstView:(id)sender;
-(IBAction)loadSecondView:(id)sender;
-(IBAction)loadThirdView:(id)sender;
#end
MultipleViewsViewController.m should be:
#import "MultipleViewsViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#implementation MultipleViewsViewController
//#synthesize firstViewController;
//#synthesize secondViewController;
//#synthesize thirdViewController;
-(IBAction)loadFirstView:(id)sender{
[secondViewController.view removeFromSuperview];
[thirdViewController.view removeFromSuperview];
[self.view insertSubview:firstViewController.view atIndex:0];
}
-(IBAction)loadSecondView:(id)sender{
[firstViewController.view removeFromSuperview];
[thirdViewController.view removeFromSuperview];
[self.view insertSubview:secondViewController.view atIndex:0];
}
-(IBAction)loadThirdView:(id)sender{
[firstViewController.view removeFromSuperview];
[secondViewController.view removeFromSuperview];
[self.view insertSubview:thirdViewController.view atIndex:0];
}
-(void)dealloc{
[firstViewController release];
[secondViewController release];
[thirdViewController release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
firstViewController = [[FirstViewController alloc] init];
secondViewController = [[SecondViewController alloc] init];
thirdViewController = [[ThirdViewController alloc] init];
[self loadFirstView:nil];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
Connect your buttons (which you haven't done in your project, which also might be the issue) and you're done.

splash screen won't show

EDIT 1: changed the code to:
delegate.h:
#import <UIKit/UIKit.h>
#class ViewController;
#interface AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
ViewController *viewController;
UIImageView *splashView;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet ViewController *viewController;
#property (nonatomic, retain) IBOutlet UIImageView *splashView;
- (void)removeSplash;
#end
delegate.m:
#import "AppDelegate.h"
#import "ViewController.h"
#implementation AppDelegate
#synthesize window;
#synthesize viewController;
#synthesize splashView;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
splashView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
splashView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Splash" ofType:#"png"]];
[window addSubview:splashView];
[window bringSubviewToFront:splashView];
[window makeKeyAndVisible];
[self performSelector:#selector(removeSplash) withObject:nil afterDelay:5.0];
[window addSubview:viewController.view];
return YES;
}
- (void)removeSplash {
[splashView removeFromSuperview];
[splashView release];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
#end
EDIT 2:
when I use:
splashView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
splashView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Splash" ofType:#"png"]];
if (splashView.image == nil) {
NSLog(#"splashView is nil");
}
it logs "splashView is nil"
My Viewcontroller is empty, just for debugging purposes.
As you probably already know, splash screens are discouraged. Since your image is Default.png, isn't it already being shown on app launch automatically?
In any case, the sleep() call is probably blocking the UI. Remove the sleep() and move the statements after it (the removeFromSuperview, etc) to another method in the app delegate. Call this method using performSelector:withObject:afterDelay:. Put the performSelector call where you currently have the sleep call.
Also, you should use the didFinishLaunchingWithOptions method instead of the old applicationDidFinishLaunching method.

Resources