passing array from one viewcontroller to another - iphone

i have a custom tabBar class in which i switch three view controllers ,i am removing the presentview controller and presenting the other .
custom tabbar class
-list
-inbox
-messages
now i have to pass an array from list to inbox
i usually create an instance of the recieving class likeInbox *inbox=[[Inbox alloc]init];
inbox.array=self.array;
but in this case its not working.the array in inbox class returns null when i nslog it

Do you inherit or use the UITabBarController in your custom tabBar?
If you want to pass a variable between list<->inbox<->messages I would suggest that you implement a method in the tabBar that can be called from the subviews. The method would send the array to the appropriate subview.

Check your property in Inbox class and check to see if self.array is not null.
Edit try: inbox.array = [NSArray arrayWithArray:self.array]

ok this worked when i passed the value to applicationdelegate and from the other class accessed from here.

Related

What would be the appropriate way to use a delegate protocol from 2 views?

I have a view controller, which uses a delegate protocol in order to pass back an array of strings. I now have another view controller, which I'd like to use the same protocol, but I've I use it I get a warning in Xcode Duplicate protocol definition of 'SearchDetailsDelegate' is ignored.
I need these two views to pass back an array for the parent view controller to parse. What would be a more appropriate way of achieve what I need to do here? Would key value observing be the way to go here?
You have few options:
rename your protocols to be different.
create an external protocol and adopt that protocol on each view
Add a property to your view called ParentController with a type of it's parent.
#property (strong,nonatomic) ParentViewController *ParentController;
(synthesise that off course)
Then, in your viewController, when you instantiate the view assign the viewController as the parent
YourView *childView = [[YourView alloc]init];
childView.parentController = self;
Now you can add a method in your viewController that can receive the strings array
-(void)setStringsArray:(NSArray*)arr{
//do what ever you need with the array
//don't forget to add this method to your .h file so it will be visible
}
Lastly send the strings array from the view:
[self.parentController setStringsArray:yourArray];
BTW
if you want to know what view send the array you can:
-(void)setStringsArray:(NSArray*)arr fromView:(UIView*)senderView{
//do what ever you need with the array
//don't forget to add this method to your .h file so it will be visible
}
and use
[self.parentController setStringsArray:yourArray fromView:self];
BTW 2
an other option will be to use notifications.
Define the protocol in a separate .h file (new file of objective c protocol) and then include it in the required view controllers.Redefining the same protocol in two different view controllers is not recommended as it has been in your case

sharing objects in Objective-C

This is a noob question. If I have a ViewController and inside that class I have an object called UserInfo and other ViewController, lets just call it X,Y,Z, etc..etc.
What do I need to do so that those X,Y,Z can use the information of UserInfo?
Well I can have another info called UserInfo inside X,Y,Z and pass UserInfo inside, but I don't think this is good OOP technique. I think inheritance is needed here... but I don't think that it is right too, as the only commonality they have is only the UserInfo
View controllers are just objects, you have to remember that.
If you're in your first view controller and it has a property NSString *aString to pass it to the next view controller you just make the second view controller's NSString *aString property equal to the first one.
Basically do this:
MyNextViewController *viewControllerTwo = [[MyNextViewController alloc] initWithNibName:nil bundle:nil];
[viewControllerTwo setAString:self.aString];
if you subclass all you inherit are the properties, and not their values.

How can one address controls dynamically in obj-c?

For instance I have created a view with 50 sequential buttons named btn1-btn50. If I want to address these buttons and make some changes to them in a loop how can I address the controls with a string name?
The typical way to get a view in a hierarchy is to call [parentView viewWithTag:] to get the view. If you give the buttons tag values from 1 to 50 you can use that to access the buttons.
If for some reason you need strings, you will have to create a custom subclass of UIButton that has a name member, assign a name to that member, then later iterate through the view hierarchy searching for an instance of your custom class with a name matching your search criteria.
If you have given them a tag, you can access them conveniently using -viewWithTag: on the parent view.
You have no guarantee that these buttons are laid out in memory sequentially, so trying to use pointer arithmetic is probably out. But you can get all the subviews of an NSView with -subviews, and just do something with the NSButtons:
for (NSView *view in [theView subviews]) {
if (![view isKindOfClass:[NSButton class]]) continue;
/// you got a button!
}
In your View Contoller add an NSMutableDictionary *buttonViews property. In your viewDidLoad method, add each button to buttonViews using the name string as the key and the button as the object. You will have to use viewWithTag: already discussed to obtain the views. Now you can locate the button using the string and benefit from the collection methods and fast enumeration. Apple's documentation for Interface Builder indicates that the "name" in IB is used to assist identifying objects in IB which is helpful for translation.

passing array from one view to another

i am a new iphone programmer i want to create an array that keeps adding a value at its last position when i moving from one view to another
because at last view ,i need that full array which has all values from all views....
i tried taking an array and sending it view to next view (array is defined in all views) but when i tried to put value to it
You can use NSMutableArray class with addObject: in order to add your views into array. Have a single view controller, declare that array and all your views can access the view controller's array.
try to save it to a class (singelton class) it will work....it will surely work if you transfer it to the delegate class....make an NSMutableArray object there and initialize it once in applicationDidFinishLaunching method and then make object of delegate class and transfer your values to it by addObject method.....
The best way is initialize your mutablearray in appdelegate and use the same using the object of delegate in each class wherever you want to use.you can add the object using addobject and can access using property objectAtIndex anywhere.

Pass a string value from one view controller to another

I am using this way to pass a string value from one view controller to another but still it's not working. Please help! From the main menu login method I pass a result value of a web service to SecondMenu:
- (IBAction)MainMenuLogin_Method:(id)sender
{
SecondMenu *lc2=[[SecondMenu alloc] initWithNibName:#"SecondMenu" bundle:nil];
lc2.username_TextField.text=#"hello";// in actual it is a soap result
//[self presentModalViewController:lc2 animated:YES];
[[[[UIApplication sharedApplication] delegate] window] addSubview:lc2.view];
}
The most likely explanation is that the username_TextField property is not linked to the actual UITextView. (1) Check that the property definition of username_TextField in the SecondMenu header is defined as an IBOutlet and that (2) it is actually linked up in Interface Builder.
Edit:
Looking at the full code at your link, it looks like the username_TextField property is a property of the Class LoginController but in this code you have instance lcv2 as Class SecondMenu. There is nothing in the code you provided to show that the SecondMenu Class has a username_TextField property. If you initialize lcv2 as an instance of LoginController the code above will work assuming you have the outlets wired correcting in interface builder.