Home Ask Login Register

Developers Planet

Your answer is one click away!

Haris February 2016

IOS Open New View controller Programatially

I need to open a registration form on my application programatically, the view controller is created on storyborad with storybpard ID ViewControllerRegister and the code I have used to open the form is

RegistrationFormViewController *registerView = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerRegister"];
          [self presentViewController:registerView animated:NO completion:nil];

But when I run the application, I am getting the error like,

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSArrayM insertObject:atIndex:]: object cannot be nil' * First throw call stack:


Badal Shah February 2016

1) Make sure about your storyboardname is "Main" or different.

enter image description here

2) Check you have entered proper storyboard identifier.

enter image description here

and Finally,

3) Sometimes Xcode can't able to find storyboard with self.storyboard. Try to mention storyboard with actual name like,

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];

        RegistrationFormViewController *registrationFormViewController = [storyBoard instantiateViewControllerWithIdentifier:@"RegistrationFormViewController"];
        [self presentViewController:registrationFormViewController animated:YES completion:nil];

Anbu.Karthik February 2016

[__NSArrayM insertObject:atIndex:]: object cannot be nil, the error says Look in your code for any lines that say "insertObject:atIndex:". The object you're inserting is apparently nil.

Make sure you have initialized your array called XXXX before adding any object into it...

NSMutableArray *xxx=[[NSMutableArray alloc]init];

and stop adding any nil object into it...

if(str) //or if(str != nil) or if(str.length>0)
//add the str into xxx array.
 //add the empty value to your xxx array

Umair Aamir February 2016

There is no problem in your code for loading a view controller programmatically. You must check with breakpoint that either your viewDidLoad method is being called in your opening controller or not. If it is being called then everything is fine and you need to check what you are doing in your opening controller's life cycle methods.

Post Status

Asked in February 2016
Viewed 1,363 times
Voted 12
Answered 3 times


Leave an answer

Quote of the day: live life