Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 9, 2010, 11:08 PM   #1
phillipie99
macrumors newbie
 
Join Date: Jul 2010
Int into a UILabel

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.
phillipie99 is offline   0 Reply With Quote
Old Jul 10, 2010, 12:46 AM   #2
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
Code:
label.text = [NSString stringWithFormat:@"%d", A];
You need a book to learn Objective-C.
PhoneyDeveloper is offline   0 Reply With Quote
Old Jul 10, 2010, 02:19 PM   #3
phillipie99
Thread Starter
macrumors newbie
 
Join Date: Jul 2010
Quote:
Originally Posted by PhoneyDeveloper View Post
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.
phillipie99 is offline   0 Reply With Quote
Old Jul 10, 2010, 02:47 PM   #4
zerocustom1989
macrumors regular
 
Join Date: Sep 2007
Quote:
Originally Posted by PhoneyDeveloper View Post
Code:
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.
zerocustom1989 is offline   0 Reply With Quote
Old Jul 10, 2010, 04:06 PM   #5
phillipie99
Thread Starter
macrumors newbie
 
Join Date: Jul 2010
Quote:
Originally Posted by zerocustom1989 View Post
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
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
Code:
//
//  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

Last edited by phillipie99; Jul 10, 2010 at 04:26 PM.
phillipie99 is offline   0 Reply With Quote
Old Jul 10, 2010, 04:28 PM   #6
zerocustom1989
macrumors regular
 
Join Date: Sep 2007
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:
Code:
//
//  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
zerocustom1989 is offline   0 Reply With Quote
Old Jul 10, 2010, 04:29 PM   #7
phillipie99
Thread Starter
macrumors newbie
 
Join Date: Jul 2010
Quote:
Originally Posted by zerocustom1989 View Post
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 is offline   0 Reply With Quote
Old Jul 10, 2010, 04:31 PM   #8
phillipie99
Thread Starter
macrumors newbie
 
Join Date: Jul 2010
But I had the IBOutlets in the line above as shown in an iPhone programming book and they are connected with Interface Builder.
phillipie99 is offline   0 Reply With Quote
Old Jul 10, 2010, 04:36 PM   #9
zerocustom1989
macrumors regular
 
Join Date: Sep 2007
Quote:
Originally Posted by phillipie99 View Post
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.

Last edited by zerocustom1989; Jul 10, 2010 at 04:41 PM.
zerocustom1989 is offline   0 Reply With Quote
Old Jul 10, 2010, 04:45 PM   #10
phillipie99
Thread Starter
macrumors newbie
 
Join Date: Jul 2010
Quote:
Originally Posted by zerocustom1989 View Post
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
phillipie99 is offline   0 Reply With Quote
Old Jul 10, 2010, 05:10 PM   #11
zerocustom1989
macrumors regular
 
Join Date: Sep 2007
Quote:
Originally Posted by phillipie99 View Post
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.
zerocustom1989 is offline   0 Reply With Quote
Old Jul 10, 2010, 05:38 PM   #12
phillipie99
Thread Starter
macrumors newbie
 
Join Date: Jul 2010
Quote:
Originally Posted by zerocustom1989 View Post
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.
phillipie99 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
IOS 7 UILabel Size Question lukin iPhone/iPad Programming 3 Aug 15, 2013 12:31 PM
Writing value to UILabel Kapthehat iPhone/iPad Programming 6 Jul 19, 2013 08:32 AM
UILabel Help lukin iPhone/iPad Programming 3 Sep 18, 2012 07:36 PM
C++ Overloading - How do I call (int, ...) instead of (int, int, ...)? ArtOfWarfare Mac Programming 7 Jun 18, 2012 04:00 PM
Stupid UILabel question mngoose iPhone/iPad Programming 7 Jun 11, 2012 08:58 PM

Forum Jump

All times are GMT -5. The time now is 05:38 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC