How to access an Objective C variable from different view controllers? - xcode

I need to call:
#property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *buttons;
from viewcontroller x but be able to access and set the button colors from view controller y. Basically I'm making a settings page that allows different color schemes. Any ideas? Thanks!

You can use delegation.
Basically, viewcontroller y would be a delegate of viewcontroller x, and every time someone changes the settings page, you viewcontroller x would notify viewcontroller y of that change. X would notify Y like so:
[delegate doSomething withParameter: parameter]
Viewcontroller y would then perform certain methods with that parameter (the variable you're trying to pass).
There are a couple of other things involved, so you should read up on delegation

You need to pass references to your view controller X when instantiating view controller Y:
ViewControllerY *viewController = [[[ViewControllerY alloc] initWithNibName:#"ViewControllerY" bundle:nil] autorelease];
viewController.viewControllerX = myRefToViewControllerX; //declare a property on your ViewControllerY
//show view controller Y

Related

How to send a text to a label in another viewcontroller in xcode?

I'm new to the xcode and i cant say i know very much about it and here's my problem. I'm trying to set a text in a label from a tableViewController to a ViewController but explanation.label setText:#"blabla" doesnt appear to be work i get a blank screen. Label is in the VC.
i dont know if it's clear enough for you to be able to help but i'm stuck.
Thank you.
You need to assign the variable public so other obj-c files can "see" it. Be sure your views #import each other, and check out this thread
First store the data in FirstTableViewController.m :
NSUserDefaults *x = [NSUSerDefaults standardUserDefaults ];
[x setObject : "putyourtext/yourselectedindexpath"] forKey : #"giveaname"];
Then u call the data in the other controller SecondViewController.m :
NSUserDefaults *x = [NSUserDefaults standardUserDefaults];
NSString *mystring = [x stringForKey:#"giveaname"];
self.nameLabel.text = mystring;

tried to change the NSArrayController selectIndex programmatically, won't work

I have a question regarding the cocoa binding.
I have a xib file, inside this xib file (named AddInformation.xib), I have added a array controller. I have bound the array to one of my NSMutableArray.
I have also set an IBOutlet for this array controller so that I can manipulate the selection of the array controller
#property (weak) IBOutlet NSArrayController *arrayController;
so, somethere in the code, when I called:
myViewController = [[NSViewController alloc] initWithNibName:#"AddInformation" bundle:nil];
BOOL b = [arrayController setSelectionIndex: 2]; // b is returned as NO
it doesn't work, it always return the first object in the array, even thought I changed the selectionindex to 2 (I have more than 10 objects in the array)
am I missing something?

How do I change values of another UITableViewController from a different UITableViewController, everything on the same view?

In a View I have this structure
vista1 UIView -> tabla1 UITableView -> tabla1_controller UITableViewController
vista2 UIView -> tabla2 UITableView -> tabla2_controller UITableViewController
I use views because depending of the orientation I move around the views.
Everything works fine, each tableviewcontroller gets data from sqlite and show the custom cells.
I want to add a behavior, when the user selects a cell in the second table, I want to change the content of the first table.
The first tableView gets information from a NSArray, how do I change
So how can I make this happen?
this is the second tableviewcontroller
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
/* I dunno how to reference the nsarray of the first table here */
first_table_array = ....
[tabla1 reloadData];
}
In this case is with two tableviewcontroller, but the real cuestion is,
How can I execute functions / change variables of another tableviewcontroller from one viewcontroller?
For example, in the tabla2_controller (UITableViewController) how can I execute [tabla1_controller reload] when the user clicks on a row of table2,
if both tables are in each own view and both views showed at the same time?
In tabla2_controller, create a variable like below
in .h file
#property (nonatomic, retain) UITableViewController *table1Ref;
in .m file
#synthesize table1Ref;
Then, when you create tabla2_controller, set tabla1_controller as value for this variable like below.
tabla2_controller *t2c = [[UITableViewController alloc] init];
t2c.table1Ref = t1c; //t1c is tabla1_controller
Now, in didSelectRowAtIndexPath of tabla2_controller, do the following.
tabla1_controller *t1c = self.table1Ref;
[t1c.tableView reloadData];

Cocoa Objective-C adding subviews

I have a game application (cocoa, not cocoa touch) where I am trying to add map elements. My app window is named mainWin. I have a subview of mainWin named viewGameMap, which I added in IB. I have a class called room.h/room.m which basically takes dimensions generated in the app for width and height of the room.
I can do this:
room* thisRoom = [[room alloc] initWithFrame: NSMakeRect(441.0-roomWidth,520.0-roomLength, roomWidth * 10, roomLength * 10)];
[[mainWin contentView] addSubview:thisRoom];
But I really want to add this subview to viewGameMap, can I subview a subview? In IB I noticed that a subview doesn't have a contentView so I'm not sure how I would place it there.
In a related question, these room views wouldn't be permanent so how do I destroy them once I am finished with them.
Thanks
First, if you declare viewGameMap in your .h file, like this (using #property directive):
#property (nonatomic, retain) IBOutlet UIView *viewGameMap;
or even just in actual declaration, like this:
IBOutlet UIView *viewGameMap;
then you should be able to bind viewGameMap to appropriate UIView within IB. This will give you direct access to viewGameMap.
Second, since you are using alloc to instantiate your Room object (n.b., class Room should be capitalized per convention), you are responsible for it (you own it). But when you call -addSubview:, then [mainWin contentView] also owns thisRoom. So, you can do this:
room* thisRoom = [[room alloc] initWithFrame: NSMakeRect(441.0-roomWidth,520.0-roomLength, roomWidth * 10, roomLength * 10)];
[viewGameMap addSubview:thisRoom];
[thisRoom release];
Later, when thisRoom is removed from [mainWin contentView], its reference count will (barring some other referencing) drop to zero and it will, ultimately, get deallocated.

Can't get subview animation to appear even after alloc :)

I have a subview loaded into an UIView. In the subview's .m file I have the following:
- (void)startAnimation {
// Array to hold png images
imageArray = [[NSMutableArray alloc] initWithCapacity:22];
animatedImages = [[UIImageView alloc] initWithImage:viewForImage];
// Build array of images, cycling through image names
for (int i = 1; i < 22; i++){
[imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"image%d.png", i]]];
}
animatedImages.animationImages = [NSArray arrayWithArray:imageArray];
// One cycle through all the images takes 1 seconds
animatedImages.animationDuration = 2.0;
// Repeat forever
animatedImages.animationRepeatCount = 0;
// Add subview and make window visible
[viewForMovie addSubview:animatedImages];
// Start it up
animatedImages.startAnimating;
NSLog(#"Executed");
}
Please be noted that I have in the .h file:
UIImageView *animatedImages;
NSMutableArray *imageArray;
UIView *viewForMovie;
#property(nonatomic,retain)IBOutlet UIView *viewForMovie;
and in the .m file:
#synthesize viewForMovie;
and I have connected viewForMovie to a UIView in IB. I've been on this for several hours now and have tried many variations I've found on the web but cannot get it to work. There are no errors and the other GUI graphics in the subview appear very nicely....but the animation just doesn't appear over top where it should. Also the NSlog reports that the method has in fact been called from the parent. Can anyone see any blaring issues? Thx.
PS: I'm pretty new at this.
Based on the code shown and the behavior you see so far, here are my suggestions:
Make sure the viewForMovie IBOutlet is connected properly in Interface Builder. If it's not connected properly (and so nil), nothing will appear. If you didn't mean to make it an IBOutlet in the first place, then you'll need to manually create it and add it as a subview to self before using it.
Not sure why you have the viewForMovie UIView in the first place. Is this subview's class (let's call it MySubview) a subclass of UIView? You can just show the animation in self instead of adding another subview inside it. Are you going to add more uiviews to this subview besides the viewForMovie?
To get rid of the "may not respond to" warning, declare the startAnimation method in the MySubview.h file (under the #property line):
-(void)startAnimation;
The fact that the warning says "UIView may not respond" also tells you that the parent view has declared newView as a UIView instead of MySubview (or whatever you've named the subview class). Change the declaration in the parent from UIView *newView; to MySubview *newView;.
In the initWithImage, what is "viewForImage"? Is it a UIImage variable or something else?
If all of the images are the same size and fit in the subview as-is, you don't need to set the frame--the initWithImage will automatically size the UIImageView using the init-image dimensions.
Double check that the images you are referencing in the for-loop are named exactly as they are in the code and that they have actually been added to the project.
Finally, you should release the objects you alloc in startAnimation. At the end of the method, add:
[imageArray release];
[animatedImages release];
The only item, however, that I think is actually preventing the animation from appearing right now is item 1.

Resources