I have two views. The first one is having 2 buttons and second one is having a label and a button. Am trying to change the text of the label based on the button pressed. In the code below, am calling an instance of second view and trying to change text in the label. But the problem is the text is not changing. Will appreciate if some one can help me here
#interface firstview : UIViewController {
IBOutlet UIButton *button1;
IBOutlet UIButton *button2;
}
#property(nonatomic, retain) IBOutlet UIButton *button1;
#property(nonatomic, retain) IBOutlet UIButton *button2;
-(IBAction)push:(UIButton *)sender;
#end
#import "firstview.h"
#import "secondview.h"
#implementation firstview
#synthesize button1;
#synthesize button2;
-(IBAction)push:(UIButton *)sender{
button1.tag = 1;
button2.tag = 2;
if(sender.tag == button1.tag){
secondview *v2 = [[secondview alloc]initWithNibName:#"secondview" bundle:Nil];
v2.title =#"first button";
v2.l1.text = #"BUTTON1";
[self.navigationController pushViewController:v2 animated:YES];
[v2 release];
}
else if(sender.tag == button2.tag){
secondview *v2 = [[secondview alloc]initWithNibName:#"secondview" bundle:Nil];
v2.title =#"Select";
v2.l1.text = #"BUTTON2";
[self.navigationController pushViewController:v2 animated:YES];
[v2 release];
}
}
#end
second view
#import <UIKit/UIKit.h>
#interface secondview : UIViewController {
IBOutlet UIButton *b2;
IBOutlet UILabel *l1;
}
#property(nonatomic, retain)IBOutlet UIButton *b2;
#property(nonatomic, retain)IBOutlet UILabel *l1;
-(IBAction)pop:(id)sender;
#end
#import "secondview.h"
#implementation secondview
#synthesize b2;
#synthesize l1;
-(IBAction)pop:(id)sender{
}
#end
At the time you are trying to set the label text, the view has not been loaded in your second view controller, so the label is nil.
Try moving the calls to after you push the view controller, or, better still (since only a view controller should change its views properties) have string properties on the second view controller for the label values, and set the label text value inside viewWillAppear.
From jrturton: The view is not been loaded in your second view controller, so the label is nil. What can you is declare a NSString property in secondview and you can set value of this property from firstview and then you can set this value to the label in viewWillAppear or viewDidLoad method.
Related
In my iOS application, I have a main View Controller with three buttons, which work like a tab bar: when I click one of the buttons, a new view controller is invoked.
I tried to implement this via container view containers, so I tried following this guide (http://www.thinkandbuild.it/working-with-custom-container-view-controllers/) and invoke the presentDetailController method in the viewDidLoad of the main controller.
Actually, no views are showed: someone can help me figuring out why? Thanks.
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIButton *btnOne;
#property (weak, nonatomic) IBOutlet UIButton *btnTwo;
#property (weak, nonatomic) IBOutlet UIButton *btnThree;
#property (weak, nonatomic) IBOutlet UIView *detailView;
- (IBAction)click:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#import "FirstViewController.h"
#interface ViewController ()
#property UIViewController *currentDetailViewController;
#end
#implementation ViewController
#synthesize btnOne, btnTwo, btnThree;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
FirstViewController *fvc = [[FirstViewController alloc]initWithString:#"I'm the first Controller!"];
[self presentDetailController:fvc];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)click:(id)sender
{
// button selection stuff
[self addDetailController:sender];
}
- (void)presentDetailController:(UIViewController*)detailVC{
//0. Remove the current Detail View Controller showed
if(self.currentDetailViewController){
[self removeCurrentDetailViewController];
}
//1. Add the detail controller as child of the container
[self addChildViewController:detailVC];
//2. Define the detail controller's view size
detailVC.view.frame = [self frameForDetailController];
//3. Add the Detail controller's view to the Container's detail view and save a reference to the detail View Controller
[self.detailView addSubview:detailVC.view];
self.currentDetailViewController = detailVC;
//4. Complete the add flow calling the function didMoveToParentViewController
[detailVC didMoveToParentViewController:self];
}
- (void)removeCurrentDetailViewController{
//1. Call the willMoveToParentViewController with nil
// This is the last method where your detailViewController can perform some operations before neing removed
[self.currentDetailViewController willMoveToParentViewController:nil];
//2. Remove the DetailViewController's view from the Container
[self.currentDetailViewController.view removeFromSuperview];
//3. Update the hierarchy"
// Automatically the method didMoveToParentViewController: will be called on the detailViewController)
[self.currentDetailViewController removeFromParentViewController];
}
- (CGRect)frameForDetailController{
// newFrame's height should be currentFrame's height minus buttons' height
CGRect detailFrame = CGRectMake(0, 0, self.detailView.bounds.size.width, self.detailView.bounds.size.height-self.btnOne.frame.size.height);
return detailFrame;
}
- (void)addDetailController:(id)sender {
FirstViewController *detailVC = [[FirstViewController alloc]initWithString:#"First button clicked"];
[self presentDetailController:detailVC];
}
#end
FirstViewController.h
#import <UIKit/UIKit.h>
#class ViewController;
#interface FirstViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *label;
#property (weak, nonatomic) IBOutlet UIButton *btnOne;
#property (weak, nonatomic) IBOutlet UIButton *btnTwo;
#property (weak, nonatomic) IBOutlet UIButton *btnThree;
- (id)initWithString:(NSString*)string;
#end
FirstViewController.m
#import "FirstViewController.h"
#interface FirstViewController (){
NSString *text;
}
#end
#implementation FirstViewController
- (id)initWithString:(NSString*)string {
self = [super init];
if(self){
text = string;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.label.text = text;
}
#end
As part of creating FirstViewController, you are calling [super init] which produces a bare UIViewController. From your diagram, it seems as if you want to load a FirstViewController from your storyboard instead.
I suggest a sequence where you create your controller using instantiateViewControllerWithIdentifier:, then set a string property in the controller that you want to use as the label, and finally assign the string to the label when the controller's view loads.
I have a program with multiple views, one of them has labels (LabelView for this question), the other has text fields(TextView for this question). I would like to take the info from the text fields, and put it on the label, I think the easiest way to do this by calling a method LabelView from TextView.
I have tried a couple things:
I tried putting a method in LabelView that changed the label's values with parameters. Then calling the method in TextView and passing the data through the parameters.
I tried making a delegate, using this question. Unfortunately the last step (add settingView.viewControllerDelegate=self to the viewDidLoad method) failed. settingView was an undeclared identifier.
Does anyone know what I should do?
EDIT: Here is some I have done, using Xman's Parent/Child view idea. This is in TextView.m:
NSInteger curScore = [self.ToP1Score.text integerValue];
NSInteger curPhase = [self.ToP1Phase.text integerValue];
self.ToP1Score.text = [NSString stringWithFormat:#"%d", (curScore += [self.player1Txt.text integerValue])];
if ([self.player1Txt.text integerValue] < 50) {
self.ToP1Phase.text = [NSString stringWithFormat:#"%d", (curPhase += 1)];
}
Two ways to achieve this.
1) Make ChildView as a childView of ParentView so that you can directly access ParentView properties in ChildView as following
ParentView.h
#interface ParentView : UIView
#property (nonatomic,strong) UITextField *mytextField;
#end
ChildView.h
#import "ParentView.h"
#interface ChildView : ParentView
#property (nonatomic,strong) UILabel *myLabel;
#end
2) Use Notification
Put this in FirstView where the textField is :
[[NSNotificationCenter defaultCenter] postNotificationName:#"sendData" object:yourTextFiels userInfo:nil];
Put this in SecondView where your Label is.
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(changeLabelValue:) name:#"sendData" object:nil];
and also this method
-(void)changeLabelValue(NSNotification *)iRecognizer {
UITextField *myTextField = iRecognizer object];
//Here You can change the value of label
}
Hope this will help you.
suppose you are passing some text from firststViewController to secondViewController,
then in firststviewcontroller add these lines of code
-(IBAction)goToNextScreen:(id)sender
{
secondViewController *newVc = [[secondViewController alloc]initWithNibName:#"secondViewController" bundle:nil];
// or if you are using storyboard
// NewViewController *newVc = [self.storyboard instantiateViewControllerWithIdentifier:#"identifier"];
newVc.text = #" Your text ";
[self.navigationController pushViewController:newVc animated:YES];
}
in 2ndViewController.h file
#import <UIKit/UIKit.h>
#interface secondViewController : UIViewController
{
NSString text;
}
#property(nonatomic, retain)NSString text;
#property (weak, nonatomic) IBOutlet UILabel *lbl;
#end
in .m file
#import "secondViewController.h"
#interface secondViewController ()
#end
#implementation secondViewController
#synthesize text,lbl;
- (void)viewDidLoad
{
lbl.text = text;
}
#end
hope this will help you.. Dont forget to up vote...
i'm new to developing with xcode,and i'm following the apple's tutorial:"Second's ios app tutorial". I've add a button to my scene "Add Sighting View Controller",and i want to switch this view to another(BirdsViewController)that have a list of bird's names. in my storyboard i've create a segue between the button and BirdsViewController and also connect the button to touch up inside, but when i run the app the button doesn't appear.Can anybody help me?thank you.
here's my code(i don't have implemented any other methods):
AddSightingViewController.h
#class BirdSighting;
#interface AddSightingViewController : UITableViewController
#property (weak, nonatomic) IBOutlet UITextField *birdNameInput;
#property (weak, nonatomic) IBOutlet UITextField *locationInput;
#property (strong, nonatomic) BirdSighting *birdSighting;
#property (strong, nonatomic) UIButton *showBirds;
-(IBAction)displayBirds:(id)sender;
AddSightingViewController.m
#import "AddSightingViewController.h"
#import "BirdSighting.h"
#import "BirdsViewController.h"
#import <UIKit/UIKit.h>
#class BirdSighting;
#interface AddSightingViewController ()
#end
#implementation AddSightingViewController
#synthesize showBirds;
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
if((textField == self.birdNameInput) || (textField == self.locationInput)) {
[textField resignFirstResponder];
}
return YES;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([[segue identifier] isEqualToString:#"ReurnInput"]) {
if ([self.birdNameInput.text length] || [self.locationInput.text length])
{
BirdSighting *sighting;
NSDate *today = [NSDate date];
sighting = [[BirdSighting alloc] initWithName:self.birdNameInput.text
location:self.locationInput.text date:today];
self.birdSighting = sighting;
}
}
}
BirdsViewController *viewController;
-(IBAction)showBirds:(id)sender {
viewController =
[[BirdsViewController alloc]
initWithNibName:#"BirdsViewController" bundle:nil];
[[self navigationController] pushViewController:viewController animated:YES];
}
I dont see any allocation and adding of button as subview to the viewcontroller's view.
I think in the app like other textfields, button is also IBOutlet. Declare button as an IBOutlet and connect the outlet to the button in the interface builder file.
#property (weak, nonatomic) IBOutlet UIButton *showBirds;
Or allocate the button and add as subview to viewcontroller's view.
Basically I want to remove my subview when a button is pressed, here is my code, please tell me what should I add.
I have created a view based application and following are the codes:
//My file name is poo1
//This is poo1ViewController.h file
#import <UIKit/UIKit.h>
#interface poo1ViewController : UIViewController
{
IBOutlet UIButton *fl;
}
#property (nonatomic,retain) UIButton *fl;
-(IBAction) ifl:(id)sender;
#end
This is poo1ViewController.m file
#import "poo1ViewController.h"
#implementation poo1ViewController
#synthesize fl;
-(IBAction) ifl:(id) sender {
UIViewController* flipViewController = [[UIViewController alloc] initWithNibName:#"flip" bundle:[NSBundle mainBundle]];
[self.view addSubview:flipViewController.view];
}
Now similarly I have added a UIViewController Subclass called "flip" with xib. And in flip.h I have added the below code
#import <UIKit/UIKit.h>
#interface flip : UIViewController
{
IBOutlet UIButton *bb;
}
#property (nonatomic,retain)UIButton *bb;
-(IBAction)ss:(id)sender;
#end
And in flip.m
#import "flip.h"
#implementation flip
#synthesize bb;
-(IBAction)ss:(id)sender
{
//What do I need to add here to return to previous view when button is pressed.
}
When this bb button is pressed, it should act as back button, how to implement it.
just add [self removeFromSuperview]; to your method ss:
Note however that this does not dealloc the view from the memory. It's just not showing!
I'd like to display 5 Buttons in a ScrollView Box but unfortunately the code I wrote has no effects on my UIButtons (I can't Scroll up or Down , can you tell me why ?
Thanks for you help, I spent a lot of hours on it (Rookie) but i still can't find the solution...
My Code :
FirstViewController.h :
#import <UIKit/UIKit.h>
#interface FirstViewController : UIViewController <UIScrollViewDelegate> {
IBOutlet UIScrollView *scroll;
UIButton *button;
UIButton *button2;
UIButton *button3;
}
#property (nonatomic, retain) IBOutlet UIButton *button;
#property (nonatomic, retain) IBOutlet UIButton *button2;
#property (nonatomic, retain) IBOutlet UIButton *button3;
#property(nonatomic,retain) IBOutlet UIScrollView *scroll;
#end
FirstViewController.m :
#import "FirstViewController.h"
#implementation FirstViewController
#synthesize button;
#synthesize button2;
#synthesize button3;
#synthesize scroll;
- (IBAction)goToViewTwo {
}
- (void)viewDidload {
[super viewDidLoad];
[scroll setScrollEnabled:YES];
[scroll setContentSize:CGSizeMake (500, 1000)];
}
- (void)dealloc {
[super dealloc];
[button release];
[scroll release];
}
#end
Hierarchy in FirstView.xib:
View
Action Sheet
Text View
ScrollView
Button
Button
Button
About the Outlets :
Scrollview is linked to File's Owner with "Scroll"
View is linked to File's Owner as a "view"
I suppose you're implementing the scroll in Interface Builder and that the app is for iPad (for the contentSize). See if you have the same configuration in IB and in code, especially that [scroll setScrollEnabled:YES];