Xcode 4 - Tab Bar application - Problem in executing third tab - ipad

I have Xcode 4 and I created an application using the Tab Bar template (and not View based app). There is a UISwitch in each of these tabs and when I change it, a UILabel switches between ON and OFF. Very simple app and no confusion. Xcode 4 creates two tabs for me by default. I need a third tab as well, so I drag TabBarItem from the Objects Library and drop it on the existing TabBarController. I create a new file, subclass of UIViewController and the following code goes into three tabs.
The following is the interface
#import <UIKit/UIKit.h>
#interface FirstViewController : UIViewController {
UISwitch *switch1;
UILabel *status1;
}
#property (nonatomic,retain) IBOutlet UISwitch *switch1;
#property (nonatomic,retain) IBOutlet UILabel *status1;
- (IBAction) switch1Change;
#end
The following is the implementation
#import "FirstViewController.h"
#implementation FirstViewController
#synthesize switch1;
#synthesize status1;
- (IBAction) switch1Change
{
if (switch1.on)
status1.text = #"ON";
else
status1.text = #"OFF";
}
The same code repeats for SecondViewController and ThirdViewController with ivars changing to switch2,status2 and switch3,status3. The link to the project is here
When I run it on the simulator, everything works fine for the first and second tab. When I open the third tab, I get the following error "Terminating app due to uncaught exception 'NSUnknownKeyException', reason: [ setValue: forUndefinedKey:]: this class is not key value coding-complaint for the key switch3."
When I remove the UISwitch from the ThirdView.xib, I don't get this error. Only when I add the switch control, I get this error. Can somebody please explain what is happening?

In Interface Builder, your third view controller is of class UIViewController (and doesn't have outlets for status3 or switch3). Change its class to ThirdViewController, wire up the outlets, and it should work.

Related

Referencing outlet not showing up in xcode 6

I am trying to write my first hello world in xcode. I have two labels and two buttons in my view, and my AppDelegate.h file is like this:
#interface QuizAppDelegate : NSObject <UIApplicationDelegate>
{
int currentQuestionIndex;
// The model objects
NSMutableArray *questions;
NSMutableArray *answers;
// The view objects
IBOutlet UILabel *questionField;
IBOutlet UILabel *answerField;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
- (IBAction)showQuestion:(id)sender;
- (IBAction)showAnswer:(id)sender;
#end
Now i am trying to add reference to my label. I go to LaunchScreen.xib and right click on the label. According to the tutorial, i have to see "answerField" and "questionField" under the "Outlets" tab, but nothing shows up. Can anyone tell me why this can be happening?
Thanks
A few things:
1) That code should really be in a view controller and not the application delegate
2) You probably should be looking in the Main.storyboard file and not LaunchScreen.xib
3) Make sure that File's Owner is set to the correct view controller on Main.storyboard after you move that code into a view controller.

Simple math app terminated in Xcode- Why?

Alright, so I'm trying to do something very, very simple: Multiplying the user input from one textfield by another textfield and feeding out the answer through a label. There are no error messages, but when I try to run the app, the simulator screen goes black and Xcode says this:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key outputlabel.'
I have no idea why it is doing this. Any suggestions? Please be extremely specific as I am new to programming.
My code:
//
// ViewController.h
// Cramer
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
// Objects are given addresses:
#property (weak, nonatomic) IBOutlet UITextField *box_a;
#property (weak, nonatomic) IBOutlet UITextField *box_b;
#property (weak, nonatomic) IBOutlet UILabel *hiLabel;
#property (weak, nonatomic) IBOutlet UIButton *clickButton;
#end
AND
//
// ViewController.m
// Cramer
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// Math takes place:
- (IBAction)clickButton:(id)sender {
NSInteger number1 = [self.box_a.text integerValue];
NSInteger number2 = [self.box_b.text integerValue];
NSInteger prod = number1 * number2;
self.hiLabel.text = [NSString stringWithFormat:#"%#", #(prod)];
}
#end
It seems like in your view controller XIB file, created using the Interface Builder or Storyboard graphic editor, you have some view (probably a UILabel) connected to a "outputlabel" property of the File Owner (that is your ViewController class) which doesn't exist.
Typically this happens when you initially connect an object from the XIB to a new outlet in the code, then you rename the outlet in the code but you forget to reconnect the object in the XIB to the new reanmed outlet. It could be, in your case, that you initially connected the output label to a IBOutlet property called "outputlabel", then you renamed it to "hiLabel" but you forgot to reconnect the label in the XIB to the new "hiLabel" IBOutlet.
To verify this you can look at all your objects in the view controller graphical interface, right-click on each of them and see their outlet connections and check that they point to properties effectively defined in the class.
your label on your storyboard is hooked up to a UILabel in your view controller named "outputlabel". However in your view controller you have it named "hiLabel". If, in interface builder, you click on your label then go to view -> utilities -> connection inspector you'll see the connections on the right hand side and should be able to verify what it's connected to. Either delete the connection and start over or change the name of your label in your view controller.
In the last line [NSString stringWithFormat:#"%#", #(prod)];, why are you using %#? NSInteger is a primitive type, not an object. It seems to me you should use a %d so that the line would read [NSString stringWithFormat:#"%d", prod];.
ETA: Use %ld instead of %d.

UITableView in UIViewController in Xcode5 not working

I've done this successfully before in past apps under Xcode4 but it doesn't seem to work anymore in Xcode5. In short, I like to add a UITableView into a UIViewController. Part of the process requires that I add as properties into the cell.h any GUI elements that it contains such as UILabel. The problem is, I'm no longer able to ctrl-drag those elements into the cell.h.
Main.storyboard
UIViewController, UITableView, UITableViewCell
The UITableView has the UIViewController as datasource and delegate.
Please assume I'm using the correct delegate methods in the ViewController.m file.
testViewController.h
#import <UIKit/UIKit.h>
#import "testCell.h"
#interface testViewController : UIViewController
<UITableViewDataSource, UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *myTableView;
#end
testViewController.m
#synthesize myTableView;
testCell.h
Issue: Unable to ctrl-drag a UILabel from the cell into this file as I've done on past apps.
testCell.m
Nothing added.
I got it to work by manually typing in the IBOutlet property and linking it that way.
ctrl-drag still does not work FYI.

Button doesn't work after Xcode update to version 4.5

I have updated my Xcode and suddenly all Buttons, which are connected in the storyboard to the selectors, doesn't work anymore. All programmatically coded Buttons and gesture recognizers work. Partly, they are calling the same IBActions.
What have I done...
Just add a Button and a Label to the View in the Storyboard. In the View Controller .h I added a Outlet to the Label and declare the IBAction.
The files:
.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *myLabel;
-(IBAction)button_pressed:(id)sender;
-(IBAction)swipe_active:(id)sender;
#end
.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize myLabel;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UISwipeGestureRecognizer *swipe_left = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipe_active:)];
swipe_left.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipe_left];
}
-(IBAction)button_pressed:(id)sender{
myLabel.text=#"Button is Running";
}
-(IBAction)swipe_active:(id)sender{
myLabel.text=#"Swipe is Running";
}
#end
And just the Button does not work.
StoryBoard: https://i.stack.imgur.com/1hruM.png
I had this same exact problem after upgrading to xcode 4.5. The IBActions appear as though they are set up correctly in IB but if you look at the .h and .m files, you'll see they're not. If you go to your storyboard and click on your view controller, look under the outlets and you'll see a tab for "Received Actions". Hook up your controls to your actions there and they'll work again.
I have been seeing the exact same issue for many of our apps. In all of my cases it was fixed by reconnecting the action in Interface Builder.

Why is my IBOutlet being released under ARC?

The Problem
An IBOutlet is released before I have a chance to use it.
What I Want
I want to access a navigation controller from my app delegate so I can reload a table view.
My Setup
I have:
A Main.xib that's set as my main interface in target settings
An IBOutlet to the navigation controller as an ivar on my app delegate
This IBOutlet hooked up to the correct navigation controller in Main.xib
App Delegate is instantiated in the xib but not set as File's Owner
I'm using ARC, Xcode 4.3.2 and iOS5.1
What I've Tried
Changing deployment target
Putting a break point on dealloc for the navigation controller, app delegate - they're never called
Reading everything I can find on ARC and IBOutlets - nothing seems to contradict what I'm doing
Creating a fresh project with just a the minimum classes required - I see exactly the same problem
Code
KPAppDelegate.h
#interface KPAppDelegate : UIResponder <UIApplicationDelegate> {
IBOutlet KPBrowseExpensesNavigationController *nc;
}
#property (strong) IBOutlet KPBrowseExpensesNavigationController *nc;
KPAppDelegate.m
#implementation KPAppDelegate
#synthesize nc;
-(void)setNc:(KPBrowseExpensesNavigationController *)nc_ {
nc = nc_; // This gets called on view load and nc gets set.
}
...snip...
// This is called about 5 seconds after app startup
-(void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects {
// By the time we get here, nc is nil.
UITableViewController *tvc = [[nc viewControllers] objectAtIndex:0];
[[tvc tableView] reloadData];
}
#end
UPDATE
I must be doing something really silly here. Even an incredibly simple project still shows this problem. See link below.
Download a simple test project that shows the problem.
In Window nib, set the FilesOwner Class as UIApplication and then point it's delegate from Outlets to the AppDelegate object. This is what is wrong in your project example.
is your outlet from the Interface Builder set as an KPBrowseExpensesNavigationController type?
If not it is not going to create the connection between your nib and ViewController.
You should set its Custom Class as KPBrowseExpensesNavigationController in the Identity Inspector
I am not sure why you declare it as a property & a non-property. I should do something like this:
#interface KPAppDelegate : UIResponder <UIApplicationDelegate>
#property (nonatomic, strong) IBOutlet KPBrowseExpensesNavigationController *nc;
And in your implementation:
#implementation KPAppDelegate
#synthesize nc = _nc; // So you don't accidentally use nc
...snip...
// This is called about 5 seconds after app startup
-(void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects {
// By the time we get here, nc is nil.
UITableViewController *tvc = [[**self.nc** viewControllers] objectAtIndex:0];
[[tvc tableView] reloadData];
}
#end
Hope this helps!
I didn't see where you alloc your nav controller. Just declaring the property won't assign any value to it, so it would be nil. In you -didFinishLaunchingWithOptions in the app delegate, set your alloc/init statement. Everything else looks fine.
KPBrowseExpensesNavigationController *nc = [[KPBrowseExpensesNavigationController alloc] init];
If you have a custom init, you can use that too, but just make sure to set it up before you try and use it.

Resources