PDA

View Full Version : Int into a UILabel




phillipie99
Jul 9, 2010, 11:08 PM
Ok so I've seen how to do this one or two time in other forums but I cannot get the int to display. I have IBActions set up so when a button is pressed it adds to int A (A+=1) but how would I go about displaying int A in UILabel Label1. I know this a simple question but, im stuck here and cannot find anything in the apple UILabel class pages.



PhoneyDeveloper
Jul 10, 2010, 12:46 AM
label.text = [NSString stringWithFormat:@"%d", A];

You need a book to learn Objective-C.

phillipie99
Jul 10, 2010, 02:19 PM
You need a book to learn Objective-C.
I have a few, I know I should use them but I like trying something first then refering to the books, but I would much rather ask a live person over trying to find a line or two of code in a book to complete a simple program. I am slowly reading through the books though.

zerocustom1989
Jul 10, 2010, 02:47 PM
label.text = [NSString stringWithFormat:@"%d", A];

You need a book to learn Objective-C.

Eh? what? I think that's a valid way of doing this.

Perhaps you haven't used Interface builder to connect your view controller with the label?

Have you declared the label in a header? Have you done the @property and @synthesize for it?

Have you then connected the label to the controller via IB?

Also, you will need to connect the button to your incremental action.

You'll learn this if you work through the first 2-3 chapters of an iPhone programming book. It's super useful.

phillipie99
Jul 10, 2010, 04:06 PM
Eh? what? I think that's a valid way of doing this.

Perhaps you haven't used Interface builder to connect your view controller with the label?

Have you declared the label in a header? Have you done the @property and @synthesize for it?

Have you then connected the label to the controller via IB?

Also, you will need to connect the button to your incremental action.

You'll learn this if you work through the first 2-3 chapters of an iPhone programming book. It's super useful.
Yup i've done all of that the only thing might be that I connected them through the delegate by accident after seeing the @synthesize and the @ property for the window in there. Should I move my outlets to my view controller. Here is the code //
// PitchCounterAppDelegate.m
// PitchCounter
//

#import "PitchCounterAppDelegate.h"


@implementation PitchCounterAppDelegate



@synthesize window;
@synthesize tabBarController;
@synthesize PitchCount;
@synthesize Team1Item;
@synthesize Team2Item;
@synthesize Settings;
@synthesize Ball;
@synthesize Strike;
@synthesize Reset;
@synthesize Count2Team1;
@synthesize Count2Team2;
@synthesize BallCount;
@synthesize StrikeCount;
@synthesize TotalPitches;
@synthesize Team2Name;
@synthesize Team1Name;
@synthesize string;

int A=00;
int B=00;
int C=00;

#pragma mark -
#pragma mark Application lifecycle
- (IBAction)changeBallCount:(id)sender {
A+=00;
B+=01;
C+=(A+B);
NSLog(@"Balls %i",B+0);
NSLog(@"Strikes %i",A+0);
NSLog(@"Total %i",A+B);

}
- (IBAction)changeStrikeCount:(id)sender {
A+=01;
B+=00;
C+=(A+B);
NSLog(@"Balls %i",B+0);
NSLog(@"Strikes %i",A+0);
NSLog(@"Total %i",A+B);
//Changes the StrikeCount Label
UILabel *StrikeCount = [[UILabel alloc] init];
StrikeCount.text = [NSString stringWithFormat:@"%d", A];
}



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

// Add the tab bar controller's view to the window and display.
[window addSubview:tabBarController.view];
[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, called instead of applicationWillTerminate: when the user quits.
*/
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
/*
Called as part of 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.
See also applicationDidEnterBackground:.
*/
}


#pragma mark -
#pragma mark UITabBarControllerDelegate methods

/*
// Optional UITabBarControllerDelegate method.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
}
*/

/*
// Optional UITabBarControllerDelegate method.
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed {
}
*/


#pragma mark -
#pragma mark Memory management

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
/*
Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
*/
}


- (void)dealloc {
[tabBarController release];
[window release];
[PitchCount release];
[Team1Item release];
[Team2Item release];
[Settings release];
[Ball release];
[Strike release];
[Reset release];
[Count2Team1 release];
[Count2Team2 release];
[BallCount release];
[StrikeCount release];
[TotalPitches release];
[Team2Name release];
[Team1Name release];
[string release];
[super dealloc];
}

@end


//
// PitchCounterAppDelegate.h
// PitchCounter
//


#import <UIKit/UIKit.h>
#import <CoreGraphics/CoreGraphics.h>

@interface PitchCounterAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {

UIWindow *window;
UITabBarController *tabBarController;
IBOutlet UITabBarItem *PitchCount;
IBOutlet UITabBarItem *Team1Item;
IBOutlet UITabBarItem *Team2Item;
IBOutlet UITabBarItem *SettingsItem;
IBOutlet UIButton *Ball;
IBOutlet UIButton *Strike;
IBOutlet UIButton *Reset;
IBOutlet UIButton *Count2Team1;
IBOutlet UIButton *Count2Team2;
IBOutlet UILabel *BallCount;
IBOutlet UILabel *StrikeCount;
IBOutlet UILabel *TotalPitches;
IBOutlet UITextField *Team2Name;
IBOutlet UITextField *Team1Name;
NSString *string;

}


@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@property (nonatomic, retain) UITabBarItem *PitchCount;
@property (nonatomic, retain) UITabBarItem *Team1Item;
@property (nonatomic, retain) UITabBarItem *Team2Item;
@property (nonatomic, retain) UITabBarItem *Settings;
@property (nonatomic, retain) UIButton *Ball;
@property (nonatomic, retain) UIButton *Strike;
@property (nonatomic, retain) UIButton *Reset;
@property (nonatomic, retain) UIButton *Count2Team1;
@property (nonatomic, retain) UIButton *Count2Team2;
@property (nonatomic, retain) UILabel *BallCount;
@property (nonatomic, retain) UILabel *StrikeCount;
@property (nonatomic, retain) UILabel *TotalPitches;
@property (nonatomic, retain) UITextField *Team2Name;
@property (nonatomic, retain) UITextField *Team1Name;
@property (nonatomic, copy) NSString *string;

- (IBAction)changeBallCount:(id)sender;
- (IBAction)changeStrikeCount:(id)sender;



@end

zerocustom1989
Jul 10, 2010, 04:28 PM
Im assuming you're using Interface builder to build the view you're displaying the labels on but in your header file

instead of @property(nonatomic, retain) UILabel *<name>;

It should have an IBOutlet in there. Especially if you wanna connect it with interface builder.

If you want to change a label via code without building the label programmatically it needs to be an IBOutlet so you can access it.

EDIT. In the .h file, I see you added all the IBOutlets in the declaration portion. They should all be in the @property lines.
ie:

//
// PitchCounterAppDelegate.h
// PitchCounter
//


#import <UIKit/UIKit.h>
#import <CoreGraphics/CoreGraphics.h>

@interface PitchCounterAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {

UIWindow *window;
UITabBarController *tabBarController;
UITabBarItem *PitchCount;
UITabBarItem *Team1Item;
UITabBarItem *Team2Item;
UITabBarItem *SettingsItem;
UIButton *Ball;
UIButton *Strike;
UIButton *Reset;
UIButton *Count2Team1;
UIButton *Count2Team2;
UILabel *BallCount;
UILabel *StrikeCount;
UILabel *TotalPitches;
UITextField *Team2Name;
UITextField *Team1Name;
NSString *string;

}


@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@property (nonatomic, retain) IBOutlet UITabBarItem *PitchCount;
@property (nonatomic, retain) IBOutlet UITabBarItem *Team1Item;
@property (nonatomic, retain) IBOutlet UITabBarItem *Team2Item;
@property (nonatomic, retain) IBOutlet UITabBarItem *Settings;
@property (nonatomic, retain) IBOutlet UIButton *Ball;
@property (nonatomic, retain) IBOutlet UIButton *Strike;
@property (nonatomic, retain) IBOutlet UIButton *Reset;
@property (nonatomic, retain) IBOutlet UIButton *Count2Team1;
@property (nonatomic, retain) IBOutlet UIButton *Count2Team2;
@property (nonatomic, retain) IBOutlet UILabel *BallCount;
@property (nonatomic, retain) IBOutlet UILabel *StrikeCount;
@property (nonatomic, retain) IBOutlet UILabel *TotalPitches;
@property (nonatomic, retain) IBOutlet UITextField *Team2Name;
@property (nonatomic, retain) IBOutlet UITextField *Team1Name;
@property (nonatomic, copy) NSString *string;

- (IBAction)changeBallCount:(id)sender;
- (IBAction)changeStrikeCount:(id)sender;



@end

phillipie99
Jul 10, 2010, 04:29 PM
Im assuming you're using Interface builder to build the view you're displaying the labels on but in your header file

instead of @property(nonatomic, retain) UILabel *<name>;

It should have an IBOutlet in there. Especially if you wanna connect it with interface builder.

If you want to change a label via code without building the label programmatically it needs to be an IBOutlet so you can access it.

Thanks ill go change those now, and let you know if it all works

phillipie99
Jul 10, 2010, 04:31 PM
But I had the IBOutlets in the line above as shown in an iPhone programming book and they are connected with Interface Builder.

zerocustom1989
Jul 10, 2010, 04:36 PM
But I had the IBOutlets in the line above as shown in an iPhone programming book and they are connected with Interface Builder.

Ah, hmm. My programming book shows em in the property lines....

perhaps it doesnt matter as long as they're connected.

This line is wrong:
UILabel *StrikeCount = [[UILabel alloc] init];

Why are you Allocating a new label? You've already synthesized strikeCount so you can access like:

self.strikeCount.text =[NSString stringWithFormat:@"%d", A]

EDIT:
Do an NSlog to make sure strikeCount.text has been changed. If the label display still doesn't change, than it has to be a connection issue.

Edit2/warning: I think by doing all of this in the appdelegate you'll run into problems, especially with the tabcontroller there.

Typically the appdelegate would manage the tabbarcontroller while additional view controllers would manage the content of the tabs. Idk, maybe you're doing something different, but if you haven't worked through books I figured I'd say it.

I dont have much experience myself, hope im still helping.

phillipie99
Jul 10, 2010, 04:45 PM
Ah, hmm. My programming book shows em in the property lines....

perhaps it doesnt matter as long as they're connected.

This line is wrong:
UILabel *StrikeCount = [[UILabel alloc] init];

Why are you Allocating a new label? You've already synthesized strikeCount so you can access like:

self.strikeCount.text =[NSString stringWithFormat:@"%d", A]

EDIT:
Do an NSlog to make sure strikeCount.text has been changed. If the label display still doesn't change, than it has to be a connection issue.

Edit2/warning: I think by doing all of this in the appdelegate you'll run into problems, especially with the tabcontroller there.

Typically the appdelegate would manage the tabbarcontroller while additional view controllers would manage the content of the tabs. Idk, maybe you're doing something different, but if you haven't worked through books I figured I'd say it.

I dont have much experience myself, hope im still helping.

Ill go change it all into the view controller. What book do you have? I have iPhone development for dummies 2nd edition. It is a little outdated though as it refrences ios2 :P

zerocustom1989
Jul 10, 2010, 05:10 PM
Ill go change it all into the view controller. What book do you have? I have iPhone development for dummies 2nd edition. It is a little outdated though as it refrences ios2 :P

Ah Yea, definitely. iOS2 is when the appstore first came out. Definitely update!

I recommend. Beginning iPhone development by DaveMark and Jeff LaMarche.
Hopefully they have a 4.0 one out now.

phillipie99
Jul 10, 2010, 05:38 PM
Ah, hmm. My programming book shows em in the property lines....

perhaps it doesnt matter as long as they're connected.

This line is wrong:
UILabel *StrikeCount = [[UILabel alloc] init];

Why are you Allocating a new label? You've already synthesized strikeCount so you can access like:

self.strikeCount.text =[NSString stringWithFormat:@"%d", A]

EDIT:
Do an NSlog to make sure strikeCount.text has been changed. If the label display still doesn't change, than it has to be a connection issue.

Edit2/warning: I think by doing all of this in the appdelegate you'll run into problems, especially with the tabcontroller there.

Typically the appdelegate would manage the tabbarcontroller while additional view controllers would manage the content of the tabs. Idk, maybe you're doing something different, but if you haven't worked through books I figured I'd say it.

I dont have much experience myself, hope im still helping.

Removing that one line did the trick thanks and ill have to look into that book.