unrecognized selector sent to instance + crash when pressing UIBarButtonItem - ios

I get the NSException "unrecognized selector sent to instance" and then my app crashes whenever the btnReload UIBarButtonItem is pressed. I included the code for what the method does for the action, however the crash/exception occurs whether or not the testChangingViewController method has code.
All of the following code is in my UIViewController class.
- (void)loadView //use loadView because not a storyboard based GUI.
{
[super loadView];
UIBarButtonItem *btnReload =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(testChangingViewController:)];
self.navigationController.topViewController.navigationItem.leftBarButtonItem = btnReload;
btnReload.enabled=TRUE;
//self.view = [[UIView alloc] initWithFrame:[[UIScr een mainScreen] bounds]];
[self.view setBackgroundColor:[UIColor whiteColor]];
[self setTitle:#"Navigation"]; //navigation bar title tied to the view controller it is managing.
UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"change_user-512"]];
[friendsIcon setFrame:CGRectMake(60, 130, 70, 70)];
[self.view addSubview:friendsIcon];
UIImageView *statusIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"talk-512"]];
[statusIcon setFrame:CGRectMake(180,130, 70, 70)];
[self.view addSubview:statusIcon];
UIView *square = [[UIView alloc] initWithFrame:CGRectMake(250, 250, 50, 50)];
[square setBackgroundColor:[UIColor orangeColor]];
[self.view addSubview:square];
// Do any additional setup after loading the view.
}
-(void)testChangingViewController
{
UITableViewController *newViewController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
[self.navigationController pushViewController:newViewController animated:YES];
}

You assign the UIBarButtonItem the #selector(testChangingViewController:) (colon at end, one parameter) but you define the method testChangingViewController (no colon, no params). The selector it is looking for that takes a parameter is not present, hence your error. These target selectors always tend to have one parameter, the "sender", that is, the UIControl that generated the event.

Related

No UIView appearing in the interface

NOTE: Jump to end for answer.
I am new to IOS Programming. How can I add a visible UIView to the following code. So far it just has the nav bar appear, and black space below, no imageView or the square UIView in the code below.
Below is code in my HomeScreenViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:([UIImage imageWithContentsOfFile:#"change_user-512"])];
[friendsIcon setFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:friendsIcon];
UIView *square = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
[square setTintColor:[UIColor orangeColor]];
[self.view addSubview:square];
// Do any additional setup after loading the view.
}
Below is code in AppDelegate.
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.myViewController = [[HomeScreenViewController alloc] init];
UINavigationController *navBar = [[UINavigationController alloc] initWithRootViewController:self.myViewController];
navBar.title = #"Navigation";
self.window.rootViewController = navBar;
[self.window makeKeyAndVisible];
return YES;
}
EDIT:
Here is also the interface for appDelegate. This is literally the only code in the proj other than the generated lines upon creating the project in xcode. I also deleted the storyboard file along with its info in the plist file (otherwise the app would crash).
#import <UIKit/UIKit.h>
#import "HomeScreenViewController.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (nonatomic, strong) HomeScreenViewController *myViewController;
#end
EDITAgain:
No Need to Read Comments, just read this:
Yes, viewDidLoad does invoke.
The background changes to red if i set background color.
No I am not using storyboard.
Still unanswered.
http://postimg.org/image/h5ayao2q3/
FINAL EDIT: Finally! problem solved. It was as simple as setting the background colour for the UIView square. D'oh.
- (void)loadView
{
[super loadView];
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.view setBackgroundColor:[UIColor redColor]];
UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:([UIImage imageWithContentsOfFile:#"change_user-512"])];
[friendsIcon setFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:friendsIcon];
UIView *square = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[square setBackgroundColor:[UIColor orangeColor]];
[self.view addSubview:square];
// Do any additional setup after loading the view.
}
Thank you very much everyone for your help. I guess my UIImage is borked, but I can figure that out myself.
Try changing it:
self.myViewController = [[HomeScreenViewController alloc] init];
TO:
self.myViewController = [[HomeScreenViewController alloc] initWithNibName:#"HomeScreenViewController" bundle:nil];
And Make sure there are some label or some thing in HomeScreenViewController. Other part of your code looks fine. Hope this helps. :)
First put breakpoint and verify that ViewDidLoad method is getting called.
If not , since you mentioned that you are creating the views programmatically you need to override the - (void)loadView method and create the view for the view controller.
-(void)loadView {
self.view = [[UIView alloc] init];
UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:([UIImage imageWithContentsOfFile:#"change_user-512"])];
[friendsIcon setFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:friendsIcon];
UIView *square = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
[square setBackgroundColor:[UIColor orangeColor]];
[self.view addSubview:square];
}
Changed the line [square setTintColor:[UIColor orangeColor]]; to [square setBackgroundColor:[UIColor orangeColor]];
right click your UIViewController and check view connection is established to the xib or storyboard view.
ViewDidload will not get called unless view loads from storyboard or xib.
loadView method gets called if you are creating UIView programatically
If you are not using story board or XIB then you need to add the following method
-(void)loadView{
//load the views
}
I have two options to solve this problem:
1) If you have XIB file corresponding to the controller:
HomeScreenViewController *homeScreenViewController = [[HomeScreenViewController alloc] initWithNibName:#"HomeScreenViewController"] bundle:nil];
1a. Verify the name of your XIB is correct.
1b. Goto XIB file of HomeScreenViewController -> File Owner -> Connection Inspector -> Check the view outlet is connected.
2) If you do it programmatically, add this to the end of your loadView function:
self.view = square;
so the final code should look like:
- (void)loadView
{
//[super loadView];
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // <-- look at here, your view has changed its frame now
[self.view setBackgroundColor:[UIColor redColor]];
UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:([UIImage imageWithContentsOfFile:#"change_user-512"])];
[friendsIcon setFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:friendsIcon];
UIView *square = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
[square setTintColor:[UIColor orangeColor]];
[self.view addSubview:square];
// Do any additional setup after loading the view.
}

how to show the views added in view controller

I had created the the view programatically but it does not display on the screen
and gives a message in the console that
Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps
the designated entry point is not set?
Here is my code
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] ;
exmTableViewController *vc =[[exmTableViewController alloc] init];
self.window.rootViewController=vc;
self.window.backgroundColor =[UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
*Code In Viewdidload *
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *img =[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
self.imageView=img;
[self.view addSubview:self.imageView];
UILabel *lbl =[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
lbl.text =#"Hello";
self.view =lbl;
}
Instead this line self.view =lbl;, use [self.view addSubview:lbl]; to add subview to your view.
try this
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *img =[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
self.imageView=img;
[self.view addSubview:self.imageView];
UILabel *lbl =[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
lbl.text =#"Hello";
[self.view addSubview:lbl];
}
-(void)viewDidLoad
{
[super viewDidLoad];
UIImageView *img =[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 20)]; [self.view addSubview:img];
UILabel *lbl =[[UILabel alloc] initWithFrame:CGRectMake(10, 200, 100, 20)];
lbl.text =#"Hello";
[self.view addSubview:lbl];
}
Try like this. self.view = lbl; in this line you assign self.view is as label you want to add subview [self.view addSubview:yourSubView]; do like this.
When you are creating View controller from storyboard, view controller is initiated by storyboard. You should not create windows root controller by yourself. You need to provide story board name in Plist.
After loading view viewDidLoad method will be called. Here you need to set up view(Adding and configuring subviews). But you are changing viewControllers view property, that is set by storyboard. If you want to change your view controllers view you can use,
- (void)loadView
// inside this method you can assign new view to view property.
If you are adding view as subview use view's method
- (void)addSubview:(UIView *)view

UISlider crashes app when adding programmatically to view

I have a custom overlay for camera, it that overlay I have UISlider:
#implementation LWOverlayView
- (id)initWithFrame:(CGRect)frame imagePath:(NSString*) imagepath{
if ((self = [super initWithFrame:frame])) {
// Clear the background of the overlay:
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
UIImageView *overlayGraphicView = [[UIImageView alloc] init];
overlayGraphicView.contentMode = UIViewContentModeScaleAspectFill;
[overlayGraphicView setImageWithURL:[NSURL URLWithString:imagepath]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
overlayGraphicView.frame = CGRectMake(80, 140, 160, 120);
[self addSubview:overlayGraphicView];
UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 436, 320, 44)];
[toolbar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismiss)];
CGRect frame = CGRectMake(0.0, 0.0, 200.0, 10.0);
UISlider *slider = [[UISlider alloc] initWithFrame:frame];
[slider addTarget:self action:#selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
[slider setBackgroundColor:[UIColor clearColor]];
slider.minimumValue = 0.0;
slider.maximumValue = 50.0;
slider.continuous = YES;
slider.value = 25.0;
[toolbar setItems:[NSArray arrayWithObjects:doneButton, slider, nil]];
[self addSubview:toolbar];
}
return self;
}
App crashes:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UISlider view]: unrecognized selector sent to instance 0x20854fe0'
Any hints?
As has been pointed out, you can't directly add a UISlider (or any other UIView) directly to the list of toolbar items.
What you need to do is create a UIBarButtonItem that contains the slider.
UIBarButtonItem *sliderBtn = [[UIBarButtonItem alloc] initWithCustonView:slider];
Now add sliderBtn to the array instead of slider.
You may find you need to set the width property on sliderBtn.
You cannot add the slider to the toolbar:
[toolbar setItems:[NSArray arrayWithObjects:doneButton, slider, nil]];
That is what is breaking your code, you can only add UIBarButtonItem
Toolbar can have items that are UIBarButtonItem. UISlider is not a UIBarButtonItem. That can have something to do with the crash. Try removing the slider and see how it works.

UIScrollView and UINavigationController not working with iOS 4

i have a problem with UIScrollView and UINavigationController(s) and iOS 4. The following code works fine with iOS 5:
////////////////////////////////////////////////////////////////////////////////////
/* Scroll View */
////////////////////////////////////////////////////////////////////////////////////
mScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 88, 1024, 668)];
mScrollView.contentSize = CGSizeMake(1850, 668);
////////////////////////////////////////////////////////////////////////////////////
// First View
////////////////////////////////////////////////////////////////////////////////////
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: #"view_bg.png"]];
imgView.frame= CGRectMake(10, 50, 350, 510);
[mScrollView addSubview:imgView];
[imgView release];
mFirstView = [[[FirstView alloc] init] autorelease];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mFirstView];
navigationController.navigationBar.tintColor = [UIColor colorWithRed:79.0/255 green:143.0/255 blue:0.0/255 alpha:1];
navigationController.view.frame = CGRectMake(25, 65, 320, 480);
[mScrollView addSubview:navigationController.view];
////////////////////////////////////////////////////////////////////////////////////
// Second View
////////////////////////////////////////////////////////////////////////////////////
UIImageView *imgView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed: #"view_bg.png"]];
imgView2.frame= CGRectMake(380, 50, 350, 510);
[mScrollView addSubview:imgView2];
[imgView2 release];
mSecondView = [[[SecondView alloc] init] autorelease];
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:mSecondView];
navigationController2.navigationBar.tintColor = [UIColor colorWithRed:79.0/255 green:143.0/255 blue:0.0/255 alpha:1];
navigationController2.view.frame = CGRectMake(395, 65, 320, 480);
[mScrollView addSubview:navigationController2.view];
// finally add the scroll view to the parent view
[self.view addSubview:mScrollView];
All i do is, to add a ScrollView with 2 NavigationControllers (with a allocated root view controller) do the view. But using this code with iOS 4 does not show the right size of the ViewControllers added. The ViewControllers are the size of the whole iPad screen!!!!!
As i already said, it works on iOS 5! Even when i change the size of the views added manually it is not working, the added view always fills the whole iPad screen!! Any ideas to fix this?
Thank you in advance!
in your code you have the following
//first view you add mfirstview.view
[mScrollView addSubview:mFirstView.view];
//second view you add navigationController2.view
[mScrollView addSubview: navigationController2.view];
So i guess you have to change the first view code to do the following
[mScrollView addSubview: navigationController.view];

how can I navigate to another controller without UINavigationControler?

I have following coding in my ViewDidLoad of ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView* view = [[UIImageView alloc] initWithImage: [UIImage imageNamed: #"logo.png"]];
view.frame = CGRectMake(300, 200, 200, 350);
[self.view addSubview:view];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(300, 800, 200, 50)];
label.textColor = [UIColor blackColor];
[label setBackgroundColor:[UIColor clearColor]];
[label setFont:[UIFont systemFontOfSize:30]];
[label setText: #"Tap to Enter"];
[self.view addSubview:label];
[label release]; }
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"Hello, You Tapped !"); }
Now I want to tap and switch to another controller and I want UITabBarController their, how can I do that?
If anyone is not clear, may ask again to me
You can call the new controller as modelviewController..That is one option..and in the viewDidLoad of the newController init tabController there..
UIViewController *newViewController = [[UIViewController alloc] init];
[self presentModalViewController:newViewController animated:YES];
.Or If you want push navigation from right to left..You can animate the view of the new Controller from right to left and push the view of current controller outside to the left of the screen..another option..but I recommend first one...
Take a button on tapping which you should be navigated to another controller. Write a method for the button and in the view did load method of the new controller, initiate the tabbarcontroller

Resources