How to modify the sender ViewController from the called ViewController? - objective-c

I have a login view controller called from a Storyboard segue. When the user logs in correctly, I need to close the current ViewController, return to the caller ViewController and hide the login button.
How can I refer to the sender ViewController, to hide the button? Do I have to pass an instance of the root ViewController to the login or is there another way?

Use delegation.
For your DetaliViewControler create a protocol, like
#protocol DetailViewControllerDelegate <NSObject>
-(void)successFullyLoggedInOnController:(DetailViewController *) controller;
#end
add a delegate declaration to DetailViewController's interface like
#property (weak) id<DetailViewControllerDelegate> delegate;
Make the MasterViewController conform to the protocol.
-(void)successFullyLoggedInOnController:(DetailViewController *) controller
{
[self.loginButton setHidden:YES];
}
Now just before the MasterViewController displays the DetailViewController, doe something like
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"DetailViewSegue"]) {
DetailViewController *vc = segue.destinationViewController;
vc.delegate = self;
}
}
In the DetailViewController once the credential where entered and verified call
[self.delegate successFullyLoggedInOnController:self];
and dismiss the DetailViewController

You should use delegation. Make the VC with the button the other one's delegate. When log in is successful, you dismiss your VC and call the delegate method to hide the button.

Related

How to make dissappear the text in the textfield of 1st view controller when clicked on Logout button in 2nd view controller?

- (IBAction)loginButtonAction:(id)sender
{
[self.navigationController popToViewController:
[self.navigationController.viewControllers
objectAtIndex:self.navigationController.viewControllers.count -2]
animated:YES];
}
This is my logout code in 2nd view controller, i want to remove text
in login screen of user name and password when clicked on Logout
button.. Please can some one help me?
You can use delegate or NSNotification for this.
Delegate.
Lets you have two View Controller.. One is ViewController and other NextViewController and You want to logout from NextViewController and clear the text in ViewController.
Steps: In NextViewController.h
#import <UIKit/UIKit.h>
#protocol clearTextField <NSObject>
-(void)clearTextFieldInPreviousController: (NSString *)string;
#end
#interface NextViewController : UIViewController
#property (assign,nonatomic) id delegate;
#end
In nextViewController.m
- (IBAction)loginButtonAction:(id)sender
{
[_delegate clearTextFieldInPreviousController:#""];
[self.navigationController popToViewController:
[self.navigationController.viewControllers
objectAtIndex:self.navigationController.viewControllers.count -2]
animated:YES];
}
And In Your ViewController.m
-(void)clearTextFieldInPreviousController: (NSString *)string{
NSLog(#"Fired");
self.label.text =#"";
}
// Note before going to next controller. You will have to set the delegate.
– (IBAction)goNextButtonAction:(id)sender {
NextViewController *acontollerobject=[self.storyboard instantiateViewControllerWithIdentifier:#"NVCSID"];
acontollerobject.delegate=self; // protocol listener
[self.navigationController pushViewController:acontollerobject animated:YES];
}
you can visit this link for Demo Example
There are a number of ways to achieve this.
Use delegation to pass message to 1st viewcontroller about the logout event from 2nd view controller and reset views in the passed message implementation.
I will suggest you to make 2nd view controller as rootViewController and not to keep the login view as part of navigation stack. You can show the login controller as a modal viewController over the 2nd View controller.

Open viewController file from every Viewcontroller

I am developing e-commerce application very similar like Flipkart.
Now I can visit my application without login. I mean initially I can skip login. But when I am going to purchase any item user should be be prompted to login.
Now client's requirement is there should be login button at every page of the application so, user should be navigated to login page from every page and after successfully login he should return to perticular page from when he/she went to login page.
Any idea how can I achieve this kind of functionality?
Step 1 : Create Base class
BaseViewController.h
#import <UIKit/UIKit.h>
#interface BaseViewController : UIViewController
-(void)takeMeToLogin;
#end
BaseViewController.m
#import "BaseViewController.h"
#interface BaseViewController () {
UIView *myTabBar;
UIButton *loginButton;
}
#end
#implementation BaseViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
loginButton.tag = 5566778897;
[loginButton addTarget:self action:#selector(takeMeToLogin) forControlEvents:UIControlEventTouchUpInside];
[loginButton setBackgroundImage:[UIImage imageNamed:#"login.png"] forState:UIControlStateNormal];
loginButton.frame = CGRectMake(x,y, width, height);
[self.view addSubview:backButton];
}
-(void) takeMeToLogin {
// code here to go to Login screen
}
Step 2: Use BaseViewController as your base class.
Now whenever you create any class, you will have as below.
#interface YourViewController : BaseViewController
By default you will have #interface YourViewController : UIViewController
Let me know if you need anything else
Edit 1
Regarding your comment, Fahim's solution is also working but it has one limitation that I have to create login button on navigation bar. I can't put login button anywhere in the screen., I will say, you can add it anywhere you want. Below is how.
In YourViewController.m have below.
UIButton *buttonThatYouWantToMove = (UIButton *)[self.view viewWithTag:5566778897];
[self.view addSubview:buttonThatYouWantToMove]; // if this don't work use insertSubview:aboveSubview:
buttonThatYouWantToMove.frame = CGRectMake(x,y,width,height); // this is very important
Done!!!
Let me know if you need further explanation.
I'd create MYBaseViewController and inherit from it in all other controller. I'd create a function which adds login button to navbar right item and a function which handles the login. This way, you'll keep everything in one place.
You can do the same in UIViewController category and call the methods in proper controllers, but personally I think that if that's solution for every single UIViewController it's less pretty.
If someone has better idea, I'd gladly hear about it.
Fahim's solution is also working but it has one limitation that I have to create login button on navigation bar. I can't put login button anywhere in the screen.
I approached differently.
I have created loginViewController.
Create protocol in loginViewController file.
Put button in every viewcontroller to open loginviewcontroller file
Modally open loginview controller
Most important thing is in my Stroryboard file I took loginviewcontroller file embedded in NavigationController and my NavigationController is modally attached with every ViewController.
In LoginViewController.h
#class LoginViewController;
#protocol LoginViewControllerDelegate <NSObject>
- (void)LoginViewControllerViewDidCancel:(LoginViewController *)controller;
- (void)LoginViewControllerViewDidDone:(LoginViewController *)controller;
#end
#interface LoginViewController : UIViewController
#property (nonatomic, weak)id <LoginViewControllerDelegate> delegate;
#end
In LoginViewController.m
- (IBAction)didCancel:(UIBarButtonItem *)sender {
[self.delegate LoginViewControllerViewDidCancel:self];
}
- (IBAction)didDone:(id)sender {
[self.delegate LoginViewControllerViewDidDone:self];
}
Now same code for FirstViewController, SecondViewController etc.
FirstViewController.m
#import "LoginViewController.h"
#interface FirstViewController ()<LoginViewControllerDelegate>
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"LoginView"]) {
UINavigationController *navigationController = segue.destinationViewController;
LoginViewController *loginViewController = [navigationController viewControllers][0];
loginViewController.delegate = self;
}
if ([segue.identifier isEqualToString:#"Thirdpage"]) {
}
}
#pragma loginViewController delegate
-(void) LoginViewControllerViewDidCancel:(LoginViewController *)controller{
//Your Logic
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void) LoginViewControllerViewDidDone:(LoginViewController *)controller{
//Your Logic
[self dismissViewControllerAnimated:YES completion:nil];
}
It works for me like a charm..

Call method in different controller in objective c

I have a view controller from where I will be showing a window controller using [runmodal]. I have some textfields and button in the modal window. When i click the button i need to call a method in view controller with the collective data from window controller. How can i achieve this? Is there anything to do with custom delegate method? As I am new to Mac dev and objective c some one help me to do.
You can use the delegation pattern. You define a protocol like this:
#protocol DataProviderDelegate <NSObject>
- (NSDictionary *) retrieveData;
#end
implement this protocol in your view controller:
#interface MainViewController () <DataProviderDelegate>
#end
#implementation MainViewController {
...
- (NSDictionary *) retrieveData {
....
}
...
#end
In your window controller you define a delegate property
#interface ModalWindowController : NSWindowController
#property (nonatomic, weak) id <DataProviderDelegate> dataProviderDelegate;
#end
From the main view controller, set that property to self
modalWindow.dataProviderDelegate = self
At this point the modal window controller is able to call any method of the view controller that is defined in the DataProviderDelegate protocol - for instance:
if (self.dataProviderDelegate) {
[self.dataProviderDelegate retrieveData];
}
To dig more in the delegation pattern I suggest to google for it
You can use a delegate for this. In the ModalViewController you will have to implement a delegate that will have a method per action (textfields, buttons) :
ModalViewController.h
#class ModalViewController;
#protocol ModalViewControllerDelegate <NSObject>
- (void)modalViewControllerDelegateButtonPressed:(APPCameraOverlay *)overlay;
- (void)modalViewControllerDelegate:(APPCameraOverlay *)overlay
textFieldEdited:(NSString *)text;
#end
#interface ModalViewController : UIViewController
#property (nonatomic, weak) id <ModalViewControllerDelegate> delegate;
#end
Then, you will be able to call your delegate methods inside your ModalViewController.m :
ModalViewController.m
// The method linked to your button
- (IBAction)actionButtonPressed {
[self.delegate modalViewControllerDelegateButtonPressed:self];
}
// Your textfield method that is called when input has changed
- (void)textFieldDidEndEditing:(UITextField *)textField {
[self.delegate modalViewControllerDelegate:self
textFieldEdited:textField.text];
}
Now, you just have to set your ModalViewController delegate object in the ViewController when showing the modal controller :
ViewController.m
#import "ModalViewController.h"
// We create an extension to the class to implement the delegate protocol
#interface ViewController () <ModalViewControllerDelegate>
#end
#implementation ViewController
// This method gets called by apple when a view controller is showed (modally, pushed or embedded)
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// if the view that is showed is the ModalViewController
if ([segue.destinationViewController isKindOfClass:[ModalViewController class]]) {
ModalViewController *controller = segue.destinationViewController;
controller.delegate = self;
}
}
// You have to implement the delegate methods now :
- (void)modalViewControllerDelegateButtonPressed:(APPCameraOverlay *)overlay {
// Do whatever you want when the button is pressed on the ModalViewController
}
- (void)modalViewControllerDelegate:(APPCameraOverlay *)overlay
textFieldEdited:(NSString *)text {
// Do whatever you want when the textfield is edited
}
#end
Add the view controller as an instance variable to your (subclassed) window controller, and then when the button is clicked you can send a method to the view controller with the collective data from the window controller.

Calling method to view controller delegate, won't dismiss modal view

I have the following simple view controller class set up
#protocol ThermoFluidsSelectorViewControllerDelegate;
#interface ThermoFluidsSelectorViewController : UIViewController <UITextFieldDelegate>
#property (weak, nonatomic) id <ThermoFluidsSelectorViewControllerDelegate> delegate;
// user hits done button
- (IBAction)done:(id)sender;
#end
#protocol ThermoFluidsSelectorViewControllerDelegate <NSObject>
-(void) didFinishSelection:(ThermoFluidsSelectorViewController *)controller fluidID: (NSString *)fluidID;
#end
the 'didFinishSeletion: fluidID:' method is defined in the master view controller and should dismiss the selector view controller when called. When the done button is pressed the following method is called:
- (IBAction)done:(id)sender
{
[[self delegate] didFinishSelection:self fluidID:nil];
}
the 'done:' method gets called (checked with an alert) but 'didFinishSelection...' is not getting called so the view will not revert back to the main screen. Any ideas?
It sounds like you have not assigned your delegate in your master view controller.
You should have something like this in your master view controller which sets up the delegate:
ThermoFluidsSelectorViewController *view = [[ThermoFluidsSelectorViewController alloc] init];
view.delegate = self;
here you can see I create the view, then set the delegate of the view back to myself.
If you are not creating the Thermo... view controller programatically, but have used a storyboard, then you can set the delegate in the prepareForSegue: method of your master view controller:
// Do some customisation of our new view when a table item has been selected
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure we're referring to the correct segue
if ([[segue identifier] isEqualToString:#"MySegueID"]) {
// Get reference to the destination view controller
ThermoFluidsSelectorViewController *cont = [segue destinationViewController];
// set the delegate
cont.delegate = self;
Hope this helps.

Understanding custom Delegate

So I have an app, and in the app there is a tableView, I have a uinavigationbarbutton that presents a modal viewController. When the user hits a go button in the modal interface, I want it dismiss the modal view and get some of the information in the modal view. I will than put that info in the tableView. To do this, I wrote a custom delegate, but it doesn’t work. I included my code below. Thanks for any help.
TrackerMainViewController.h //the tableView
#import "NewItemViewController.h"
#interface TrackerMainViewController : UITableViewController <UITableViewDelegate, DetailDelegate>
TrackerMainViewController.m
-(void)finishedAddingFoodItemFromDetail:(NSDate *)date whatWasEaten:(NSString *)whatFood whichMeal:(NSString *)meal {
NSLog(#"in delegate method here");
[self.tableView reloadData];
[self dismissModalViewControllerAnimated:YES];
}
NewItemViewController.h // the modal view
#protocol DetailDelegate <NSObject>
-(void)finishedAddingFoodItemFromDetail:(NSDate *)date whatWasEaten:(NSString *)whatFood whichMeal:(NSString *)meal;
#end
#interface NewItemViewController : UIViewController {
id <DetailDelegate> _delegate;
}
#property (nonatomic, retain) id <DetailDelegate> delegate;
#end
NewItemViewController.h
#implementation NewItemViewController
#synthesize delegate = _delegate;
//the go button in the modal view
- (IBAction)Go:(id)sender {
[self.delegate finishedAddingFoodItemFromDetail:[NSDate date] whatWasEaten:#"chicken" whichMeal:#"breakfast"];
}
I put a log in both the go button and in the implementation of the delegate in the tableview, but only the go log is being called.
Thanks
In the code you posted, you dont set the delegate. You need to set it similar to this detailView.delegate = self, otherwise it is nil. You can send messages to a nil-object without any warning and error, nothing will happen.