Can't change UILabel's text from another View Controller - objective-c

I have two view controllers, one with a text field and a 'Next' button and another one with just a label. The idea is that you fill in your name in the text field and click 'Next'. It should then switch to the other view and the label should show your name.
When I switch views however, the label is just empty. I am rather new to Objective-C and I'm hoping someone knows why this is happening :).
ViewController.m:
#interface ViewController () {
IBOutlet UILabel *label;
IBOutlet UITextField *textField;
}
-(IBAction)go:(id)sender
{
label.text = textField.text;
}
-(IBAction)remove:(id)sender {
[sender resignFirstResponder];
}

You said that you have two View Controllers. The first View Controller has an instance of UITextField, and a UIButton. Your second View Controller just has an instance of UILabel.
However, I noticed in your code that ViewController.m, which I am assuming is your first View Controller, has two IBOutlets, one for a UILabel and one for a UITextField, which doesn't make sense because the UILabel is supposed to be part of your second View Controller.
What you need to do is properly delete the IBOutlet for the UILabel from your first View Controller. Then, in your second View Controller, add the IBOutlet for it's UILabel.
Then, you need to implement the prepareForSegue method in your first View Controller and add a code statement like this:
[[segue destinationViewController]label].text = textField.text;
This way, when the segue is performed, you will successfully pass your first View Controller's text field's text to your second View Controller's label.

Related

Perform segue from main view on container view

I seem to be unable to understand how to go about this. I have a button on my main view. This view contains a container view. I would like the button on the main view to make the container view segue to another view. I have set up an identifier for the segue, which goes from containerView1 to containerView2. This is a push-segue. The identifier is pushSegue.
On the button on the main view I have tried this:
- (IBAction)btnChangeLocation:(UIButton *)sender {
UIViewController *a = [[ContainerView1 alloc]init];
[a performSegueWithIdentifier:#"pushSegue" sender:nil];
}
I have successfully performed this segue from within containerView1, by just placing within it, and performing the segue from there. It works just fine then.
- (IBAction)testButton:(UIButton *)sender {
[self performSegueWithIdentifier:#"pushSegue" sender:nil];
}
But how would I go if I wanted to trigger the segue on the containerView1, from the button on the main view?
Thanks.
EDIT:
I would also like to be able to perform the same segue, from a container view, that is within the container view.
Just to summarize.
MainView----->ContainerView1-->pushSegue--->ContainerView2
ContaainerView1 has a subContainerView, which also has a button, which causes ContainerView1 to segue into ContainerView2. This button and the button on the MainView does the same thing really, just from different "locations".
EDIT: Added a picture to help explain. http://tinypic.com/r/maxpp2/8
With UIViewController *a = [[ContainerView1 alloc]init]; you are instantiating a new ContainerView1 controller. That won't help you; you need to call performSegueWithIdentifier:sender: on the instance already created.
Depending on how your Storyboard and code are set up, you need to find a way to get a hold of the embedded view controller.
For this set up:
You could do something like this in the main (hosting) view controller:
#property (weak, nonatomic) IBOutlet UIView *childView;
#property (weak,nonatomic) UINavigationController *container;
...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"embedContainer1"]) {
self.container = segue.destinationViewController;
}
}
-(IBAction)doIt:(id)sender {
[self.container.viewControllers[0] performSegueWithIdentifier:#"pushSegue" sender:nil];
}
By implementing prepareForSegue:sender:, you're able to get a reference to the child viewcontroller; cleaner then going through the array of childViewControllers IMHO.

Open 3 views on 3 buttons

I have 3 buttons on same page but those buttons are calling 3 different views on
click.Like button1 opens view1,button2 open view2 and button3 open view3.but i want all
those buttons on upside of view like tab bar is having downside.I tried by creating or
calling views on button click but on every click it's creating new object of view which i
don't want..Please help me to solve this issue.Is there any other way to solve this
problem. I tried segment too but not working.
I'm not quite sure, if this is what you really want.
The view would be larger than it actually requires to be, regarding your screenshot?
Why don't you just set the views size to match the size of the view that is below your three buttons.
In case the entire ViewControllers' view is of height: 524px
substracting the orange bar and the buttons: around 450px
Now just create the view from your buttons click action with the exact frame size, as your tableview and add it to the view as subview?
If another button is clicked, remove this view again and add the new view?
So in case you got this in one single ViewController, which I guess:
Simply change the following for your buttons:
So, in case you got the following 3 buttons
#property (weak, nonatomic) IBOutlet UIButton *buttonA;
#property (weak, nonatomic) IBOutlet UIButton *buttonB;
#property (weak, nonatomic) IBOutlet UIButton *buttonC;
Implement the function, which reacts on these three button events:
- (IBAction)myButtonAction:(UIButton *)sender
{
UIViewController *viewControllerToDisplay;
// Load UIViewController for its view from your .xib-file:
// Alternatively you could do this with switch.case and checking a button's tag-values, but I think is easier to read
if (sender == self.buttonA) viewControllerToDisplay = [ViewControllerA new];
else if (sender == self.buttonB) viewControllerToDisplay = [ViewControllerB new];
else if (sender == self.buttonC) viewControllerToDisplay = [ViewControllerC new];
// Only display the view, if the viewController was created
if (viewControllerToDisplay)
{
viewControllerToDisplay.view.frame = self.tableView.frame;
[self.view addSubview:viewControllerToDisplay];
}
}

Add, remove subview and communicate between subview and main uiviewcontroller

I have created two uiviewcontrollers in storyboard. I am adding second UIview as a subview to 1st view when a button is pressed.
Now, my subview has a done and cancel button, which upon been touched, the subview has to be removed from the main view and needs to send some data back to main view. Is using delegates the only way to solve this? Please explain if there is any other simpler or better option.
Thank you :)
It sounds like the question is just about subviews of the 1st view controller's view. In that case, the 1st view controller can directly inspect all of them. i.e. Say the data that you'd like "passed" between views is the text of a UITextField contained on the subview.
You have an outlet to the subview, probably painted in IB?
// MyViewController.m
#property(weak, nonatomic) IBOutlet UIView *subview; // self.view is it's parent
Create an outlet that connects to whatever subviews you want data from:
#property(weak, nonatomic) IBOutlet UITextField *textField; // probably, subview is it's parent
Hide and show the "dialog":
self.subview.alpha = 0.0; // to hide (alpha is better than 'hidden' because it's animatable
self.subview.alpha = 1.0; // to show
When the button is pressed:
- (IBAction)pressedDoneButton:(id)sender {
self.subview.alpha = 0.0;
// or, prettier:
[UIView animateWithDuration:0.3 animations:^{ self.subview.alpha = 0.0; }];
// the text field still exists, it's just invisible because it's parent is invisible
NSLog(#"user pressed done and the text that she entered is %#", self.textField.text);
}
The point is that data is not being passed between views. The view controller has pointers to views. Some, like buttons generate events for the view controller to react to. Others carry data that the view controller can see.

TextField will NOT resign first responder with UIModalPresentationFormSheet view

I've created a button on one viewController that loads another view modally using the UIModalPresentationFormSheet presentation style. On this loaded view, I have two textFields, and I'm forcing the first textField to be first responder so that the keyboard will appear immediately with the new view. I've set up the textFields to have an action method that is hooked up to "Did End on Exit" event. However, whenever I hit "return" on the keyboard for either textField, the keyboard fails to go away (Here is my code):
// addCustomPage method that is called when button from original view is touched
- (IBAction) addCustomPage:(id)sender
{
NSLog(#"Adding Custom Page");
if (!self.customPageViewController)
{
self.customPageViewController =
[[CustomPageViewController alloc] initWithNibName:#"CustomPageViewController" bundle: nil];
}
customPageViewController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:customPageViewController animated:YES];
// force keyboard to appear with loaded page on the first textField
[customPageViewController.firstTextField becomeFirstResponder];
}
#interface CustomPageViewController : UIViewController
#property (strong, nonatomic) IBOutlet UITextField *firstTextField;
#property (strong, nonatomic) IBOutlet UITextField *secondTextField;
- (IBAction)keyboardEndOnExit:(id)sender; // DID END ON EXIT EVENT
#end
//in CustomPageViewController.m
-(IBAction)keyboardEndOnExit:(id)sender
{
[sender resignFirstResponder];
}
This is a fairly straight forward problem, and I have no problem normally dismissing keyboards using this technique with basic views and textFields. I'm not sure if using a view in this presentation format, or set up makes things different. Thanks!
You have confirmed that you keyboardEndOnExit method is actually being called?
You could also take a more direct approach by calling [yourTextView resignFirstResponder] when a specific action is take by the user, such as a key pressed etc. I would still check if that method is ever being called using breakpoints or a log.
Have a look at this question. Pretty sure it is the same problem caused by UIModalPresentationFormSheet.

XIB Displaying SubComponent

I created a custom view, which has one button and one text field , as given below
#interface CommUICustomSignInView : CommUICustomView {
IBOutlet NSButton *pBtn;
IBOutlet NSTextField *pTextField;
NSTrackingArea *pTrackingArea;
NSCursor *pPonitHandCursor;
}
#property (nonatomic,retain)IBOutlet NSButton *pBtn;
#property (nonatomic,retain)IBOutlet NSTextField *pTextField;
All items are linked properly, with the view,
In Another window controller XIB, i have added one tab view, in one of the tab item view, i am going to add this view,
added one tab view and assigning this view as below,
NSTabViewItem *pTabViewItem = [pTabView tabViewItemAtIndex:0];
if(pOfflineCTlist == nil){
pOfflineCTlist = [[CommUIOfflineCTlist alloc]
initWithNibName:#"CommUIOfflineViewController" bundle:nil];
}
[pTabViewItem setView : [pOfflineCTlist view]];
[pTabView selectTabViewItemAtIndex:0];
Now with that, i could able to track the mouse event in customSignInview, in nstrackregion,
but i couldn't see the other controls, sign-in button and text field,
Am i doing something wrong,
Finally i found following way to do this,
1 -- Created a Resource like Button / text field in the Main view,
2 -- Put them over the custom view,
3 -- Link them with the custom view,
now i could able to see it,
Is there any other approach then please suggest me,