-1

I want to change the text of a label from another controller you know like defining a global variable.

user1540999
  • 86
  • 1
  • 3

5 Answers5

1

Define and create its property and alloc UILabel in AppDelegate

Now use that label in any where u want in whole application

AppDelegate *objAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[self.view addSubView:objAppDelegate.label]  // your gobal label added

Change its text anywhere

[objAppDelegate.label setText:@"New Text"];
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
0

There are multiple ways of doing that. You could phrase your question more specific, though.

You could make your label accessible to other view controllers. Doing so you would need to know the owning view controller from where ever you want to access the label.

Ore you could store a reference to the label in your subclass of UIApplicationDelegate and access that using [[UIApplication getSharedApplication] delegate] and access it from everywhere. If you do that do not forget to nil that reference when the lable is destroyed.

However, without knowing a bit more what you want to achieve and why, I cannot say whether this is advisable at all, making a UILable object accessible from outside its view's controller. Most probably it is not.

Hermann Klecker
  • 14,039
  • 5
  • 48
  • 71
0

a) Pass a pointer to the label to a controller B and use it to change text.

b) Define a protocol and define a delegate member in controller B. Implement the delegate method in the controller A (that owns the label) and set the text passed as a param of the delegate method. Call the delegate method in controller B and pass the text you want to set to the label.

c) Register controller A as a observer to some notification. Post notification in controller B and add text to user info dictionary. Implement a method in a controller A that receives notification and set the text passed in user info dictionary.

b) is the most reasonable most of the time. a) is not clean. c) is an overkill.

Mateusz
  • 314
  • 1
  • 5
0

With the introduction of Automatic Reference Counting with iOS 5, which forces certain programming rules and restrictions, certain programming constructs and accepted practices are no longer possible. The use of the extern is one of the C based directives that is frowned on by the linker. Actually you will get linkage errors if you try to use with ARC enabled.

However, it is still very possible to use global variables with iOS 5, you just can use the #define directive to do so.

The following steps demonstrate one possible solution for using global variables.


1) Declaring a Global Variable :

    NSString * gvar;
    @interface AppDelegate : UIResponder <UIApplicationDelegate>

2) Initializing the Global Variable :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
     gvar = [[NSString alloc] initWithString:@"Name1"];
    return YES;
}

3) Import this AppDelegate file in both your ViewControllers.


4) Assigning gvar to UILabel in your first ViewController :

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
     self.label.text= gvar;
}

5) Change its value from your second ViewController :

gvar = [self.songArray objectAtIndex:indexPath.row];

Once you go back to the previous viewController, your label will dispaly the new text.

Nayan Chauhan
  • 542
  • 5
  • 16
  • I am trying to do this with a UILabel. It keeps telling giving me and error. Im not really sure why this isnt working - it makes sense to me. – Will Von Ullrich Jun 23 '15 at 04:20
-1

I think you had better use protocol to do it

cloosen
  • 993
  • 7
  • 17