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
Related
I'm trying to change UITextView.text from SecondViewController in ViewController. I can do it if it was NSString type in SecondViewController by doing:
SVC.string = #"test";
The problem is that:
messageBox.text doesn't change
SVC.messageBox.text returns (null)
One of the methods in ViewController.m:
SecondViewController *SVC = [[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
SVC.messageBox.text = #"changed";
NSLog(#"SVC: %#", SVC.messageBox.text); // result = (null)
[self presentViewController:SVC animated:YES completion:NULL];
SecondViewController.h:
#import <UIKit/UIKit.h>
#interface SecondViewController : UIViewController
#property (nonatomic) IBOutlet UITextView *messageBox;
#end
SecondViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.messageBox.text = #"first";
}
How to get over with this?
Try this,
One of the methods in ViewController.m:
SecondViewController *SVC = [[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
SVC.strMessage= #"changed";
[self presentViewController:SVC animated:YES completion:NULL];
SecondViewController.h:
#import <UIKit/UIKit.h>
#interface SecondViewController : UIViewController
{
IBOutlet UITextView *messageBox;
}
#property (nonatomic) NSString *strMessage;
#end
SecondViewController.m:
#synthesize strMessage;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.messageBox.text = strMessage;
}
You can use delegates to do it as well.
Example:
Viewcontroller2nd.h
#protocol SecondViewControllerDelegate <NSObject>
#optional
-(void) changeLabel:(NSString*)str;
#end
#interface ViewController2nd : UIViewController{
IBOutlet UIButton *bttn;
id <SecondViewControllerDelegate> delegate;
}
#property (retain) id delegate;
#end
Viewcontrollerone.h
#interface ViewController : UIViewController <SecondViewControllerDelegate>
{
IBOutlet UILabel *lbl;
}
-(IBAction)passdata:(id)sender;
#end
Viewcontrollerone.m
-(void) changeLabel:(NSString*)str{
lbl.text = str;
}
More about delegates:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html
Hope this helps...
The problem with your code is your accessing the uicontrols which are not yet created in the memory.
You have to change the order of the execution of your statements like this...
SecondViewController *SVC = [[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
[self presentViewController:SVC animated:YES completion:NULL];
SVC.messageBox.text = #"changed";
NSLog(#"SVC: %#", SVC.messageBox.text);
Set #property (nonatomic) IBOutlet UITextView *messageBox; to #property (nonatomic, strong) IBOutlet UITextView *messageBox; and check if you have connected it with UItextView. And don't reset it in viewDidLoad. Another thing you can try is:
SecondViewController *SVC = [[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
[self presentViewController:SVC animated:YES completion:NULL];
SVC.messageBox.text = #"changed";
NSLog(#"SVC: %#", SVC.messageBox.text); // result = (null)
Along with what mentioned above.
just property-synthesize string andf just assign this string to yourTextView.text. For example see bellow..
#interface SecondViewController : UIViewController{
NSString *strMsgText;
}
#property(nonatomic, retain) NSString *strMsgText;
#end
#implementation SecondViewController
#synthesize strMsgText;
after that when you present SecondViewController at that time assign your value like bellow..
SecondViewController *SVC = [[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
SVC.strMsgText = #"changed";
[SVC.strMsgText retain];
[self presentViewController:SVC animated:YES completion:NULL];
and in viewDidLoad: method of SecondViewController just assign that string to messageBox(TextView), like bellow...
- (void)viewDidLoad
{
messageBox.text = strMsgText;
}
I have two table view in one view controller.
They works great! But they are not pushing to any vc.
Under -(void) viewDidLoad method in my main view controller:
horizontalViewController = [[HorizontalViewController alloc] init];
verticalViewController = [[VerticalViewController alloc] init];
[horizontalTableView setDataSource:horizontalViewController];
[verticalTableView setDataSource:verticalViewController];
[horizontalTableView setDelegate:horizontalViewController];
[verticalTableView setDelegate:verticalViewController];
horizontalViewController.view = horizontalViewController.tableView;
verticalViewController.view = verticalViewController.tableView;
What can I do?
Thanks.
refer a following code. If you want use a pushViewController method.
You must be have a NavigationViewController.
so, your structure is a little complex. one ViewController has number of Two TableViewController. one ViewController is not have NavigationController. NavigaitonViewController necessarily belong to the app when it runs because it should be configured.
TwoTableViewsAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UINavigationController *naviController = [[[UINavigationController alloc] initWithRootViewController:viewController] autorelease];
[window setRootViewController:naviController];
[window makeKeyAndVisible];
return YES;
}
TwoTableViewsViewController.m
- (void)viewDidLoad {
if (firstController == nil) {
firstController = [[FirstTVContoller alloc] init];
}
if (secondController == nil) {
secondController = [[SecondTVController alloc] init];
}
[firstTable setDataSource:firstController];
[secondTable setDataSource:secondController];
[firstTable setDelegate:firstController];
[secondTable setDelegate:secondController];
firstController.view = firstController.tableView;
secondController.view = secondController.tableView;
firstController.rootViewController = self;
secondController.rootViewController = self;
[super viewDidLoad];
}
FirstTVContoller.h , SecondTVController.h
#import <Foundation/Foundation.h>
#interface FirstTVContoller : UITableViewController <UITableViewDataSource, UITableViewDelegate>{
NSMutableArray *items;
}
#property (nonatomic, retain) UIViewController *rootViewController;
#end
FirstTVContoller.m , SecondTVController.m
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
VerticalDetailViewController *verticalDetailViewController = [[VerticalDetailViewController alloc] initWithNibName:#"VerticalDetailViewController" bundle:nil];
[[self.rootViewController navigationController] pushViewController:verticalDetailViewController animated:YES];
}
I have been stuck here.
I have a NSString in Appdelegate. And I have two views called firstview,second view.In first view I have a label and set text from stringvariable which is in Appdelegate .When I click on the button in first view,It goes to the secondview(added the second view to first view as a subview).In second view I have a back button.when I click the back button it is displaying the first view ( Here I am setting the value which is in the appdelegate.And then used this [self.view removeFromSuperview]).The Problem is first view is appearing but label value is not updating.Can any body tell me how to update the Labeltext.Kindly tell me.
Appdelegate.h
#import <UIKit/UIKit.h>
#class FirstView;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
NSString *str1;
}
#property (nonatomic,retain) NSString *str1;
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController *viewController;
#end
Appdelegate.m
#import "AppDelegate.h"
#import "FirstView.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize viewController = _viewController;
#synthesize str1;
- (void)dealloc
{
[_window release];
[_viewController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[FirsView alloc] initWithNibName:#"FirstView" bundle:nil] autorelease];
self.str1 = #"view1";
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
FirstView.h
#import <UIKit/UIKit.h>
#import "secondView.h"
#interface FirstView : UIViewController
{
IBOutlet UILabel *btn;
secondView *cont;
}
-(IBAction)gotoView2:(id)sender;
#end
FirstView.m
#import "FirstView.h"
-(IBAction)gotoView2:(id)sender
{
cont = [[secondView alloc] initWithNibName:#"secondView" bundle:nil];
[self.view addSubview:cont.view];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate *del = [[UIApplication sharedApplication] delegate];
[btn setTitle:del.str1];
}
**SecondView.h**
#import <UIKit/UIKit.h>
#interface SecondView : UIViewController
{
}
-(IBAction)goBack:(id)sender;
#end
SecondView
#import "SecondView.h"
#import "AppDelegate.h"
#implementation SecondView
-(IBAction)gotoView1:(id)sender
{
AppDelegate *del = [[UIApplication sharedApplication] delegate];
[del setStr1:#"Home"];
[self.view removeFromSuperView];
}
There is a pattern how to do such things. You should define a protocol like this:
#protocol FirstViewDelegate <NSObject>
- (void)didDismissSecondView;
#end
Your first view should conform to this protocol:
#interface FirstView: UIViewController <FirstViewDelegate> {
...
in its implementation add function didDismissSecondView:
- (void) didDismissSecondView
{
AppDelegate *del = [[UIApplication sharedApplication] delegate];
[btn setTitle:del.str1];
}
Your second view has to have a property
#interface SecondView : UIViewController
{
id<FirstViewDeledate> delegate;
}
#property (nonatomic, retain) id<FirstViewDeledate> delegate;
When you show your second view from the first view set its delegate to self of first view
in your function:
-(IBAction)gotoView2:(id)sender
{
SecondView *aView = [[SecondView alloc] init] // or whatever
...//other initialization code
aView.delegate = self;
... // show it
}
and before you dismiss the second view:
-(IBAction)gotoView1:(id)sender
{
AppDelegate *del = [[UIApplication sharedApplication] delegate];
[del setStr1:#"Home"];
[self.delegate didDismissSecondView];
[self.view removeFromSuperView];
}
And you done.
A bit long, but works.
(I'm beginner.)
I'm practicing Navigation Controller. I try to implement a simple calculator.
I ran the code in simulator. After I pressed any button which was linked to "addFunction", "substractFunction", "multiplyFunction" or "divideFunction", It crashed.
The debugger marked the following code in main.m
int retVal = UIApplicationMain(argc, argv, nil, nil);
and said "Thread 1: Program received signal: "SIGABRT"."
Does anyone know how to cope with this situation? Thanks.
Here's the code:
ChangeAppView.h:
#import <UIKit/UIKit.h>
#class ChangeViewController;
#interface ChangeAppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
UINavigationController *navigationController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) UINavigationController *navigationController;
#end
ChangeAppDelegate.m:
#import "ChangeAppDelegate.h"
#import "ChangeViewController.h"
#implementation ChangeAppDelegate
#synthesize window;
#synthesize navigationController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
navigationController = [[UINavigationController alloc] init];
self.window.rootViewController = navigationController;
ChangeViewController *changeViewController = [[ChangeViewController alloc] initWithNibName:#"ChangeViewController" bundle:nil];
[navigationController pushViewController:changeViewController animated:YES];
[changeViewController release];
[self.window makeKeyAndVisible];
return YES;
}
…
- (void)dealloc
{
[navigationController release];
[window release];
[super dealloc];
}
#end
CalculatorViewController.h:
#import <UIKit/UIKit.h>
#interface CalculatorViewController : UIViewController
{
IBOutlet UITextField *numberField1;
IBOutlet UITextField *numberField2;
IBOutlet UILabel *resultLabel;
}
#property (nonatomic , retain) IBOutlet UITextField *numberField1;
#property (nonatomic , retain) IBOutlet UITextField *numberField2;
#property (nonatomic , retain) IBOutlet UILabel *resultLabel;
-(IBAction)addFunction:(id)sender;
-(IBAction)substractFunction:(id)sender;
-(IBAction)multiplyFunction:(id)sender;
-(IBAction)divideFunction:(id)sender;
-(IBAction)clear:(id)sender;
-(IBAction)backgroundTap:(id)sender;
#end
CalculatorViewController.m:
#import "CalculatorViewController.h"
#implementation CalculatorViewController
#synthesize numberField1;
#synthesize numberField2;
#synthesize resultLabel;
-(IBAction)addFunction:(id)sender
{
float a = ([numberField1.text floatValue]);
float b = ([numberField2.text floatValue]);
resultLabel.text = [NSString stringWithFormat:#"%2.f" , a+b];
}
-(IBAction)substractFunction:(id)sender
{
float a = ([numberField1.text floatValue]);
float b = ([numberField2.text floatValue]);
NSString *result = [[NSString alloc] initWithFormat:#"%2.f" , a-b];
resultLabel.text = result;
[result release];
}
-(IBAction)multiplyFunction:(id)sender
{
float a = ([numberField1.text floatValue]);
float b = ([numberField2.text floatValue]);
resultLabel.text = [[NSString alloc] initWithFormat:#"%2.f" , a*b];
}
-(IBAction)divideFunction:(id)sender
{
float a = ([numberField1.text floatValue]);
float b = ([numberField2.text floatValue]);
resultLabel.text = [[NSString alloc] initWithFormat:#"%2.3f" , a/b];
}
-(IBAction)clear:(id)sender
{
numberField1.text = #"";
numberField2.text = #"";
resultLabel.text = #"";
}
-(IBAction)backgroundTap:(id)sender
{
[numberField1 resignFirstResponder];
[numberField2 resignFirstResponder];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[numberField1 release];
[numberField2 release];
[resultLabel release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
self.title = #"Calculator";
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
From the exception above, it seems that your IBActions are not properly connected.
As dasdom mentioned, delete all your buttons, create new buttons and then add IBAction methods accordingly.
Also one more thing i recognized in your code, in the multiply and divide methods there is a memory leak.You have written
resultLabel.text = [[NSString alloc] initWithFormat:#"%2.f" , a*b];
it should be
resultLabel.text = [[[NSString alloc] initWithFormat:#"%2.f" , a*b]autorelease];
or
resultLabel.text = [NSString StringWithFormat:#"%2.f" , a*b];
and do a similar change in divide method also.
To what did you link your backgroundtap method?
It seems that you buttons aren't buttons. They seem to be view. Delete the buttons from you nib and put new buttons there and link them you our IBActions.
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];
}