iPhone OBJ-C: Sharing a MutableArray and moving between two ViewControllers - iphone

Classes:
AppDelegate
AppViewController
OtherViewController
Currently the application opens in the AppViewController. I want to add a new view, the OtherViewController. However I want the a method in the AppViewController to save to a MutableArray that the OtherViewController can use to show information.
1- Where should I create the MutableArray, in the AppDelegate? And how do I then access it?
I would like the ability to swipe a object on the AppViewController to get the OtherViewController to slide on, and I would just use a back button on the OtherViewController to go back.
2- How can I switch between the Controllers?
Thanks for the help!

Create a NSMutableArray in otherViewController (say otherArray)...
Dont forget to set property for that Array.. Because it is used as getters and setters for that Object..In our case we need set the value for that Array object..
When you move from AppViewController you will present the viewcontroller as..
OtherViewController *obj=[[OtherViewController alloc] initWithNibName:#"OtherViewController" bundle:nil];
//Here
obj.otherArray = yourArrayInAppViewController;
[self presentModalViewController:obj animated:YES];
[obj release];
Just NSLog otherArray count in your ViewDidLoad of OtherViewController.. You can see the Array has been passed...

Related

Attempt to present <xViewcontroller> whose view is not in the window hierarchy

I am using the option to play some media, if I link it to an IBAction on ViewController.h it will play fine.
The problem occurs, when I try to call that, from another ViewController, for example;
ViewController *myViewController = [[ViewController alloc] init];
[myViewController showVideos];
This is called from SecondViewController and refers to the code in ViewController.m
-(void)showVideos {
[[ApplifierImpact sharedInstance] showImpact];
}
It works using it if I am viewing it on the ViewController, but the call using the
-(void)showVideos {
[[ApplifierImpact sharedInstance] showImpact];
}
Throws the error about window hierarchy when calling it from the SecondViewController.m file
Now, in the SecondViewController.h file, the only reference to ViewController, is a simple import of the .h file, should I be initialising it or giving it a property in there also?
This: ViewController *myViewController = [[ViewController alloc] init]; creates a new ViewController object. You then don't present it (i.e. don't give it access to the screen) but you ask it to showVideos.
I suspect what you really want is to get a reference to an existing ViewController. When you create your SecondViewController give it a reference to the first one to act as a delegate.

Objective-c / xcode - Access button state in different class

So I tried this in many different ways but I can't get it to work. Im trying to change the state of a UIbutton in a different class.
class1.h
#property (strong, nonatomic) IBOutlet UIButton *monthly;
class2.m
- (void)viewDidLoad
{
ViewController *vc = [[ViewController alloc] init];
vc.monthly.enabled = NO;
}
Whatever I try and where ever I put the code, the button state is not changing. When I log the state in class2.m:
NSLog(vc.monthly.enabled ? #"Yes" : #"No");
It always returns No, even if I just stated it as YES in my class2.m. Long story short: My button property is not updating from a different class. Please tell me if you need to see any more code and i'll update asap.
i think problem is with class instance. the following line create new instance
ViewController *vc = [[ViewController alloc] init];
that's why your button state is not changing you have to get reference of your previously created intstace no need to create new instance.
for this you can use AppDelegate file to declare property of class1.
see following code
AppDelegate.h
#Property(nonatomic, ratain) ViewController *vc;
AppDelegate.m
#synthesize vc;
now alloc & initialize vc whenever you need it like following.
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication
sharedApplication] delegate];
appDelegate.vc=[[ViewController alloc] init];
also don't forgot to import AppDelegate.h file where you write above code.
now using appDelegate.vc you can use all property of View Controller in all classes of you project.
The main problem is you are creating new instance each time when you are going to check the button state. But the button state is for the button which you have created in class1.h. So you have need that you should create button in Appdelegate class and fetch the instance from Appdelegate in the class where you are checking the status of button and check the status of button. I think it will help.
Access the button using the object of the ViewController class that is already in the stack. No need to creat a new object like ViewController *vc = [[ViewController alloc] init];. When you are doing this it creates a new object so you are not getting the write thing.

how to show textfields value from one view controller to another viewcontroller when button is clicked

I've implemented two uiviewcontrollers. I have some text fields in one on a uiview and 1 button on it. What I want is that when I fill text fields and click on the button, these values show on another uiview controller. I am facing a problem when I fill the text fields and click on the button, another uiview on shows on the iPhone screen but the text field values are not shown on it. I also import nextview.h file in my viewcontroller.m file. Below is some of my code:
-(IBAction)save:(id)sender
{
nextview *NView = [[nextview alloc] initWithNibName:nil bundle:nil];
[[NView name] setText:name.text];
[[NView fathername] setText:father.text];
[[NView country] setText:countryselected.text];
[[NView gender] setText:genderselected.text];
[[NView dob] setText:dateselected.text];
[[NView username] setText:username.text];
[[NView password] setText:password.text];
[[NView email] setText:email.text];
[self presentModalViewController:NView animated:YES];
}
In the nextview class, I also define #property of uilabels in the .h file and I also #synthesize, initialize and release these labels in the nextview.m file and linked these variables to all labels. The problem is still there. What can I do to avoid this problem?
See this line of code:
nextview *NView = [[nextview alloc] initWithNibName:nil bundle:nil];
You use a nil as nib name to create your next view. Have you overwritten initWithNibName of the nextview to give a nib name?
do you init you property your member(such as name) when call
nextview *NView = [[nextview alloc] initWithNibName:nil bundle:nil]; if you init them in viewdidiload or other function about view,then they will be initialized after
you call [self presentModalViewController:NView animated:YES];
There i so many method through which you can pass data from one view to another. in this Tutorial there is brief discription about this method Which you Want Check it Tutorial Source Code
See apples documentation for initWithNibName
http://developer.apple.com/library/ios/ipad/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html
The nib is not loaded until its view is accessed. For additional initialisation use viewDidLoad.
Meaning, the outlets are just not set directly after initWithNibName.
So, one of the things you could do is add a reference to the first viewController as a property in nView and in viewDidLoad of nView you set the labels according to the first VCs labels.
On a side note: your naming convention is wrong. Use Uppercase for classes and lowercase for instances of that class ie it should be
Nextview *nView;

transfer information between navigation based application

I have application
I want a button to open another screen with done button
after click this button a data be transfered from the current screen to the previous one
like opening options screen , how to let the main view know the changes be done on this subview
Best regards
You can use properties for this.
Create a property in the second class: .h file
#interface DetailViewController : UIViewController {
NSString *head;
}
#property(nonatomic,retain)NSString *head;
#end
in .m file
#synthesize head;
Now in your firstviewcontroller or first class
if (dt==nil) {
DetailViewController *d = [[DetailViewController alloc]initWithNibName:#"Detailview" bundle:nil];
self.dt = d;
[d release];
}
dt.head=itemsum;
[self.navigationController pushViewController:self.dt animated:YES];
Suppose you have to pass NSString to other class then declare one Nsstring in second class and make accessor method for nsstring and then call this code from first view controller
yournextviewcontroller *viewcontroller = [[yournextviewcontroller alloc] initWithNibName:#"yournextviewcontroller" bundle:nil];
viewcontroller.Stringname = name;
[self.navigationController viewcontroller:evernotebook animated:YES];
You can take UIView as IBOutlet in the same ViewController. Show that view with click on Button like
IBOutlet UIView *yourAnotherScreenView; //Bind this to your view in XIB file.
[self.view addSubView:yourAnotherScreenView];
[self.view bringSubViewtoFront:yourAnotherScreenView];
Take Done button on the view and wireup the action event in the same controller and do the necessary stuff as required.
Use the simple delegate concept of Objective-C .
Check very simple answers in the below post for using delegate in Objective-C .
How do I set up a simple delegate to communicate between two view controllers?

Reload a table view on close of add subview

I have three objects in the applicaion. There is a UItableviewcontroller(with nib file) that shows a list of the items. One UIviewcontroller to add item (with nib file) and a model class that contains item object.
I show the list of item firstly (on application start). I have a navigation button on navigation bar to pop up add view (add as subview) on the same screen (on table view). In add view I have a add button. when I click on add button it adds the record and disappear from the table view but doesn't reload the that.
I have used following code in add item button click action
listitem *home= [[listitem alloc] initWithNibName:#"listitem" bundle:nil];
[self.navigationController pushViewController:home animated:YES];
[home viewWillAppear:YES];
[home release];
[self.view removeFromSuperview];
In viewwillappear function I am reloading the data from database and also reloading the table view data using reloadData.
Am I doing correct. What is the mistake I am doing.
Your code is very tricky to read* but this is what I think you're doing:
You're making a new list each time that you add an item. you don't want to create a new home object, you want to go back to the last one?
i.e. replace your code with
[self.navigationController popViewControllerAnimated:YES];
and this will go back to your original list, which should have refreshed itself (a UINavigationController will call viewWillAppear for you).
Hope that helps.
NB You have to have used a navigation controller to add your 'add item' view otherwise this code won't work :( This is how you should be adding your item view.
AddItem *home = [[AddItem alloc] initWithNibName:#"AddItem" bundle:nil];
[self.navigationController pushViewController:home animated:YES];
This will slide on your add item view.
If you want the add item view to be a popup, a UINavigationController is definitely not the way to do it!
You will need to tell your initial list view when it needs to update itself. You can do this using either a delegate or a notification. I'd go for a delegate in this case.
You need to add this to your add item view controller's code (AddItem's .h file)
#interface AddItem : UIViewController {
UITableViewController *delegate;
}
#property (nonatomic, assign) UITableViewController *delegate;
#end
and synthesize it in your AddItem's .m file
#synthesize delegate;
When you create your add item view controller, set the delegate to be the initial view controller. i.e.
AddItem *home= [[AddItem alloc] initWithNibName:#"AddItem" bundle:nil];
home.delegate = self;
home.view.frame = CGRectMake(10, 20, 300, 300);
[self.view addSubview:home.view];
Finally, when you have added a new item, tell your delegate to refresh itself like so :
[delegate reloadData];
[self.view removeFromSuperview];
*It's standard practice to use capital letters for class names i.e. listitem should be called ListItem. Personally, I'd call it ItemListViewController so it's clear what it does.