Method in RootViewController not Storing Array - iphone

I have an array initialized in my RootViewController and a method that addsObjects to an array. I created a RootViewController object in my SecondViewController. The method runs (outputs a message) but it doesn't add anything to the array, and the array seems empty. Code is below, any suggestions?
RootViewController.h
#import "RootViewController.h"
#import "SecondViewController.h"
#implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
myArray2 = [[NSMutableArray alloc] init];
NSLog(#"View was loaded");
}
-(void)addToArray2{
NSLog(#"Array triggered from SecondViewController");
[myArray2 addObject:#"Test"];
[self showArray2];
}
-(void)showArray2{
NSLog(#"Array Count: %d", [myArray2 count]);
}
-(IBAction)switchViews{
SecondViewController *screen = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:screen animated:YES];
[screen release];
}
SecondViewController.m
#import "SecondViewController.h"
#import "RootViewController.h"
#implementation SecondViewController
-(IBAction)addToArray{
RootViewController *object = [[RootViewController alloc] init];
[object addToArray2];
}
-(IBAction)switchBack{
[self dismissModalViewControllerAnimated:YES];
}
EDIT*************
With Matt's code I got the following error:
" expected specifier-qualifier-list before 'RootViewController' "

You are missing some very essential fundamentals here. If you allocate a new RootViewController in your SecondViewController, it is not the same instance as the one you used to create your SecondViewController so it will not have a reference to the array you are adding objects to. What you are trying to do won't work. You must create an ivar in your SecondViewController for your RootViewController and then access it inside second view. Something like this:
-(IBAction)switchViews{
SecondViewController *screen = [[SecondViewController alloc]
initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[screen setRootViewController:self];
[self presentModalViewController:screen animated:YES];
[screen release];
}
Your ivar would need declared like this in the SecondViewController.h:
#property (nonatomic, retain) RootViewController *rootViewController;
And then synthesized in the .m
Then, you can access the ivar from within your SecondViewController:
-(IBAction)addToArray{
[[self rootViewController] addToArray2];
}

Related

Pass nsmutablearray from one viewcontroller to another view controller

Here I am adding values to nsmutablearray in the firstviewcontroller. When I click UIButton I want pass this array to another view controller.
AppointmentClass *appObj = [[AppointmentClass alloc]init];
appObj.subject = [key objectForKey:#"Subject"];
appObj.location = [key objectForKey:#"Location"];
appObj.scheduledStart = [key objectForKey:#"ScheduledStart"];
appObj.scheduledEnd = [key objectForKey:#"ScheduledEnd"];
[firstNameArray addObject:appObj];
[appObj release];
appObj=nil;
When I passing firstnamearray value to appointmentdataarray.
secondviewcontroller *appointmentViewObject = [[secondviewcontroller alloc]initWithNibName:#"secondviewcontroller" bundle:nil];
[appointmentViewObject setAppointmentDataArray:firstNameArray];
From the above the appointmentdataarray returning a null value.
[self presentModalViewController:appointmentViewObject animated:YES];
[appointmentViewObject release];
Have you allocated the array?
NSMutableArray* firstNameArray = [NSMutableArray array];
You can try one thing:
in secondviewcontroller.m add a following method:
-(id) initwithnibName:(NSString*)_nib withArray:(NSMutableArray*)_array{
self = [super initWithNibName:_nib bundle:nil];
if (self) {
self.array =_array;
}
return self;
}
and add -(id) initwithnibName:(NSString*)_nib withArray:(NSMutableArray*)_array; to secondviewcontroller.h.
Now replace secondviewcontroller *appointmentViewObject = [[secondviewcontroller alloc]initWithNibName:#"secondviewcontroller" bundle:nil];
[appointmentViewObject setAppointmentDataArray:firstNameArray];
with secondviewcontroller *appointmentViewObject = [[secondviewcontroller alloc]initwithnibName:#"secondviewcontroller" withArray:firstNameArray];
Hope this helps.
Try modifying statement:
[appointmentViewObject setAppointmentDataArray:firstNameArray];
With this:
[appointmentViewObject setAppointmentDataArray:[NSArray arrayWithArray:firstNameArray]];
use this:
secondviewcontroller.h file
NSMutableArray* AppointmentDataArray;
#property(nonatomic,retain) NSMutableArray* AppointmentDataArray;
-(void)setMyArray:(NSMutableArray *)arr;
secondviewcontroller.m file
#synthesize AppointmentDataArray;
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil
{
if ((self = [super initWithNibName:nibNameOrNil
bundle:nibBundleOrNil])) {
// Custom initialization.
AppointmentDataArray = [[NSMutableArray alloc] init];
}
return self;
}
-(void)setMyArray:(NSMutableArray *)arr{
AppointmentDataArray=arr;
}
///////When you push
secondviewcontroller *appointmentViewObject = [[secondviewcontroller alloc]initWithNibName:#"secondviewcontroller" bundle:nil];
[appointmentViewObject setMyArray:firstNameArray];
[self presentModalViewController:appointmentViewObject animated:YES];

Adding a UITabBarController and UITabs programmatically

I need to add a UItabbar programmatically to a UIView. This is the code below, the app starts from a RootViewTableController, then gets pushed to this view, but the tabs don't work maybe it's something in MainWindow.xib that's the problem?:
// TabBarController.h
#interface TabBarController : UITabBarController
#end
// TabBarController.m
#import "TabBarController.h"
#import "FirstV.h"
#implementation TabBarController
-(void)viewDidLoad {
NSMutableArray *listOfViewControllers = [[NSMutableArray alloc] init];
UIViewController *vc;
vc = [[UIViewController alloc] init];
vc.title = #"A";
[listOfViewControllers addObject:vc];
[vc release];
vc = [[UIViewController alloc] init];
vc.title = #"B";
[listOfViewControllers addObject:vc];
[vc release];
[self.tabBarController setViewControllers:listOfViewControllers animated:YES];
[super viewDidLoad];
}
#end
Screenshot here:
As you can see no tabs at the bottom, should I drag and drop a UItabbar element into the view?
Try putting your [super viewDidLoad]; ABOVE your code where it should be.

Passing a NSString from View to another

I am trying to pass a NSString to another view.
In my first view (is a map view) my code is:
FirstView.m file:
import "FlipsideViewController.h"
-(IBAction) displayDetails:(id) sender{
MyLocation *ann = [_mapView.selectedAnnotations objectAtIndex:([_mapView.selectedAnnotations count]-1)];
FlipsideViewController *flipsideViewController;
flipsideViewController= [[FlipsideViewController alloc]init];
flipsideViewController.details =ann.name;
NSLog(#"ann.name:%#", ann.name);
NSLog(#"details:%#", flipsideViewController.details);
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"details" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
and the NSlog print the correct string. But when I try to pass that string to my SecondViewController string called details:
In my FlipsideViewController.h is defined:
NSString *details;
#property (nonatomic, strong) NSString *details;
While in the FlipsideViewController.m
#synthesize details; //and whenever I put the log, it prints the null value
value: NSLog(#"**************detalli %#",detalli);
Where is my fault?
in the secondViewController add a property, a retained property of type NSString
For adding properties please read the following
http://cocoacast.com/?q=node/103
Please edit this section
FlipsideViewController *flipsideViewController;
flipsideViewController= [[FlipsideViewController alloc]init];
flipsideViewController.details =ann.name;
NSLog(#"ann.name:%#", ann.name);
NSLog(#"details:%#", flipsideViewController.details);
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"details" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
to
FlipsideViewController *flipsideViewController;
flipsideViewController= [[FlipsideViewController alloc]init];
flipsideViewController.details =ann.name;
NSLog(#"ann.name:%#", ann.name);
NSLog(#"details:%#", flipsideViewController.details);
flipsideViewController.delegate = self;
flipsideViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:flipsideViewController animated:YES];
[flipsideViewController release];
try this step:-
1)Firstly You make the property in anotherViewController.h
#property (nonatomic, strong ) NSString *fetchStr;
2) Synthesis the NSString in anotherViewController.m
#synthesize fetchStr;
3)use in firstViewController
anotherViewController *avc = [[anotherViewController alloc]init];
avc.fetchStr = ann.name;
and print(NSLog) the fetchStr in anotherViewController
made annotation
#property(nonatomic,retain) MyLocation *annotation;
and then pass it to another view controller.
it will work.

How to pass a variable to the next view?

My app has an variable called int antalratt, which is the number of correct answers in that view. Now I want to pass that variable to the next view, where I want to get the number of correct answers to be shown! I know how to get an integer to a label text though!
The int antalratt is written in the firstviewcontroller.m, how do I make it "global" so that I can use it in the secondviewcontroller?
Thanks in advance!
make a variable in the public interface of secondviewcontroller.h
#property (nonatomic, strong) NSNumber *correctAnswers;
synthesize it in .m and then pass the value of antalratt in firstviewcontroller with secondviewcontroller.correctAnswers = [NSNumber numberWithInt:antalratt];
to secondviewcontroller. then set the labeltext
Method 1:
RootViewController
-(IBAction)nextPage{
int antalratt = 12; // Value to be transfered
FirstViewController * fvc = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
fvc.answer = antalratt;
[self presentModalViewController:fvc animated:YES];
[fvc release];
}
FirstViewController
#interface FirstViewController : UIViewController
{
int answer;
}
#property(nonatomic,assign) int answer;
#implementation FirstViewController
#synthesize answer;
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"%d",answer); // //displays answer on log
}
#end
Method 2 (AppDelegate)
AppDelegate
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
int antalratt;
}
#property(nonatomic ,assign) int antalratt;
RootViewController
-(IBAction)nextPage{
int antalratt = 12; // Value to be transfered
AppDelegate * delegate = [[UIApplication sharedApplication] delegate];
delegate.antalratt = antalratt;
FirstViewController * fvc = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
[self presentModalViewController:fvc animated:YES];
[fvc release];
}
FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
AppDelegate * delegate = [[UIApplication sharedApplication] delegate];
NSLog(#"%d",delegate.antalratt); //displays answer on log
}
Method 3 (NSUserDefaults)
RootViewController
-(IBAction)nextPage{
int antalratt = 12; // Value to be transfered
[[NSUserDefaults standardUserDefaults] setInteger:antalratt forKey:#"answer"];
FirstViewController * fvc = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
[self presentModalViewController:fvc animated:YES];
[fvc release]; }
FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
int ans = [[[NSUserDefaults standardUserDefaults] objectForKey:#"answer"] intValue];
NSLog(#"%d",ans); //displays answer on log
}
filename *detailViewController = [[filename alloc] initWithNibName:#"filename" bundle:nil];
detailViewController.audio=#"yourData";
[self presentModalViewController:detailViewController animated:YES];
[detailViewController release];
Declare in filename.h
NSString *audio;
#property(nonatomic,retain) NSString *audio;
and filename.m
#synthesize audio;
-(void) ViewDidLoad
{
NSLog(#"Audio = %#",audio); // if ur variable is integer declare %d in nslog.
}
thats all
//
view1.h
#interface view1 : UIView{
NSString *passingVariable;
}
#property (nonatomic, strong) NSString *passingVariable;
#end
//
view1.m
#synthsize passingVariable;
#implementation view1
#end
// in another view
view2.m
#import "view1.h"
#implementation view2
-(IBAction)changeview
{
view1 *myview = [[view1 alloc]init];
myview.passingVariable = [NSString stringWithString:#"Hello Variable"];
[self.navigationController pushViewController:myview animated:YES];
}
#end

Transfer image across view controllers

On my iPhone app, I have two viewControllers (firstViewController, secondViewController), on firstViewController the user can select a photo from the camera roll and it then displays it in an imageView, however I need to also display it in secondViewController but have no idea how to.
Can you also please explain your answers in-depth as I am fairly new to objective-C
Here's my code:
firstViewController.h
#import <UIKit/UIkit.h>
#interface firstViewController : UIViewController
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
UIImageView *theImageView;
}
#property (nonatomic, retain) IBOutlet UIImageView *theImageView;
-(IBAction)selectExistingPicture;
#end
firstViewController.m
#import "firstViewController.h"
#import "secondViewController.h"
#implementation firstViewController
#synthesize theImageView
-(IBAction) selectExistingPicture
{
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage : (UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
theImageView.image = image;
[picker dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *) picker
{
[picker dismissModalViewControllerAnimated:YES];
}
-(IBAction)switchSecondViewController {
SecondViewController *viewcontroller = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self presentModalViewController:viewcontroller animated:YES];
[viewcontroller release];
}
// Default Apple code
#end
There's not much in secondViewController so I won't bother posting that code.
Have a method in your second view controller of the type
-(void)setImage:(UIImage *)image
In the method below after you create a viewcontroller call the method with the image as shown
-(IBAction)switchSecondViewController {
SecondViewController *viewcontroller = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self presentModalViewController:viewcontroller animated:YES];
[viewcontroller setImage:theImageView.image]
[viewcontroller release];
}
You need to declare a property in second view so you can set the image from your first view some thing like this:
secondImageView is the property in secondView and you have to set it
-(IBAction)switchSecondViewController {
SecondViewController *viewcontroller = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
viewcontroller.secondImageView.image = firstViewImage;
[self presentModalViewController:viewcontroller animated:YES];
[viewcontroller release];
}