Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-19-2010, 06:54 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 4
yangMaster97 is on a distinguished road
Default Assign text to a UIButton on another view?

Hey, newbie programmer here. How do I assign a text (or string) to a UIButton on another view? What is the code?
yangMaster97 is offline   Reply With Quote
Old 08-19-2010, 07:07 AM   #2 (permalink)
I will be a billionaire.
 
IphoneSdk's Avatar
 
Join Date: Sep 2009
Location: Scarborough, United Kingdom
Age: 17
Posts: 1,344
IphoneSdk is on a distinguished road
Default

Look into NSUserDefaults
__________________
iOS Apps | Website | Twitter | Facebook
IphoneSdk is offline   Reply With Quote
Old 08-19-2010, 08:00 AM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by yangMaster97 View Post
Hey, newbie programmer here. How do I assign a text (or string) to a UIButton on another view? What is the code?
In general, one view controller should not be mucking with another view controller's buttons directly. You really want each view controller's internals to be hidden, so that you can change it later without having to tell other objects about the changes. It would be better to add a method ("changeMessage") to the second view controller that tells the view controller to display a different message, and the second view controller then changes the button text itself. That way, if you decide, later, to change the button to a label, all you have to do is change the changeMessage method to display the message to a label. With your way, changing the UI in one view controller requires you to change the code in every other view controller that cares about it.

So, with that in mind, here's what to do.

In "SecondViewController" add a method

Code:
-(void) changeButtonMessage: (NSString*) newMessage
{
  myButton.text = newMessage
}
Make sure that method declaration is in the header so other objects can call it.

Add properties to all your view controllers in your app delegate.

When you create your view controllers, set up the properties in your app delegate to point to the view controllers.

#include the header of your app delegate in any view controller that needs to talk to other view controllers. (Let's say your app delegate is of class "MyAppDelegate".)

Now, you can use code like this anywhere in your app:

Code:
MyAppDelegate* theAppDelegate = [UIApplication sharedApplication].delegate;
OtherViewController* theOtherViewController = theAppDelegate.theOtherViewController;
[theOtherViewController changeButtonMessage: @"new button text"];
This approach works, but it isn't the greatest object oriented design. It causes "coupling" between the app controller and the view controllers. I wrote a post that explains a better way to communicate data and messages between objects:

Sharing data between view controllers and other objects
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Tags
button, string, text, view

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 342
5 members and 337 guests
freewind, givensur, lendo, Newbie123, PlutoPrime
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,894
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:54 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0