Storing info from UITextField back to detailTextLabel - ios

I have a tableViewCell that says Nickname and when you click on it, a segue takes you to a view controller with a text field where you can type a nickname, once you push the back button it should appear in the detailTextLabel of the main table view controller. I just keep getting an error I posted the code below where I get an error as well.
nickname text field code:
#import "NicknameViewController.h"
#interface NicknameViewController ()
#end
#implementation NicknameViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self.delegate updateCarNickname:self.nicknameField];
}
#end
Here is the header file for the nickname view controller:
#import <UIKit/UIKit.h>
#protocol CarNicknameDelegate <NSObject>
#optional
- (void)updateCarNickname:(UITextField *)updateNickname;
#end
#interface NicknameViewController : UIViewController
#property (nonatomic, weak) id <CarNicknameDelegate> delegate;
#property (strong, nonatomic) IBOutlet UITextField *nicknameField;
#end
In the main view controller I keep getting errors on this code and can't seem to figure it out:
-(void)updateCarNickname:(UITextField *)updateNickname {
self.nicknameCell.detailTextLabel.text = updateNickname;
}
Before the segue begins I used this code:
if ([segue.identifier isEqualToString:#"nicknameSegue"]) {
NicknameViewController *controller = segue.destinationViewController;
controller.delegate = self;
}

All views in a view controller gets deallocated once the view controller's dealloc is called.
You should change your CarNicknameDelegate's parameter UITextField to NSString.
#protocol CarNicknameDelegate <NSObject>
#optional
- (void)updateCarNickname:(UITextField *)updateNickname;
#end
and change your NicknameViewController to something like this.
#interface NicknameViewController ()
#end
#implementation NicknameViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self.delegate updateCarNickname:self.nicknameField.text];
}
#end

Related

Pass data from Collection View Controller to View Controller using delegates

On a button press, I am trying to send data from Collection View Controller to my View Controller. Here is what I have so far:
CollectionViewController.h
#import <UIKit/UIKit.h>
#class CollectionViewController;
#protocol CollectionViewDelegate <NSObject>
-(void) sendTest;
#end
#interface CollectionViewController : UICollectionViewController
#property (nonatomic, weak) id<CollectionViewDelegate> deligate;
#end
CollectionViewController.m
-(void) viewDidLoad {
[super viewDidLoad];
}
...
NSLog(#"check");
[self.deligate sendTest];
NSLog(#"called");
FooViewController.m
#import "CollectionViewController.h"
#interface FooViewController () <CollectionViewDelegate> {}
#end
#implementation FooViewController
- (void)viewDidLoad {
[super viewDidLoad];
CollectionViewController *controler = [[CollectionViewController alloc] init];
controler.deligate = self;
}
- (void) sendTest {
NSLog(#"Delegates are great!");
}
Unfortunaly when I call [self.deligate sendTest]; nothing happens. I know it is called, because I get the check, called logs.
The main wrong thing is that you are creating a CollectionViewController yourself, but you shouldn't. The CollectionViewController that you want is instantiated automagically by the Storyboard. How to find this view controller?
1 - Catch it during the segue in FooViewController:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
CollectionViewController *collectionViewController =(CollectionViewController *) segue.destinationViewController;
collectionViewController.deligate = self;
}
2 - Find it in within the child view controllers in FooViewController
self.childViewControllers
Does it help you?

Delegate not being set (set correctly)

I'm having trouble figuring out why I'm getting a nil delegate for this call. It seems like I'm setting the delegate just fine.
LogoutViewController.h
#protocol SomeNewDelegate <NSObject>
#required
- (void)someMethod;
#end
#interface LogoutViewController : UIViewController
#property (nonatomic, weak) id<SomeNewDelegate> delegate;
LogoutViewController.m
- (IBAction)logoutButtonTapped:(id)sender {
NSLog(#"logout tapped");
[self.delegate someMethod];
[self dismissViewControllerAnimated:YES completion:nil];
}
MainViewController.m
#interface MainViewController () <UIScrollViewDelegate, SomeNewDelegate>
#property (nonatomic, strong) LogoutViewController *logoutVC;
#end
#implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.logoutVC.delegate = self;
}
- (void)someMethod {
NSLog(#"someMethod");
}
someMethod never gets called and I don't see why the delegate is nil. Any help?
Use below code in viewDidload
LogoutViewController *logoutVC = [[LogoutViewController alloc]init];
logoutVC.delegate = self;
Remove LogoutViewController from property.
I have used this and its working fine.
Hope its working for you.
Edited code in .h file
#protocol syncDataDelegate <NSObject>
#required
- (void) offlineSyncProcess;
#end
#interface LoginViewController : UIViewController
{
id <syncDataDelegate> syncDelegate;
}
#property (retain) id syncDelegate;
in .m file
#implementation LoginViewController
{
// your code
}
#synthesize syncDelegate;
- (IBAction)logoutButtonTapped:(id)sender {
NSLog(#"logout tapped");
[syncDelegate offlineSyncProcess];
}
You can call delegate method from here in LoginViewController.m file from any method.
in My Delegate implementation
in .m file
#interface MainViewController ()<syncDataDelegate>
#end
#implementation MainViewController
- (void)viewDidLoad
{
[super viewDidLoad];
LoginViewController *LVC = [[LoginViewController alloc]init];
LVC.syncDelegate = self;
}
now use the delegate methode here
- (void) offlineSyncProcess
{
// your code here
}
this code is working in my project, check this in your project.
Make sure your logoutVC is not nil when you try to set the delegate property for it.
You can implement the setDelegate method in your LogoutViewController.m. Then use break point to make sure it is invoked.
Delegate is nil because your logoutVC is nil, you haven't allocated memory to the logoutvc property.
Just before self.logoutVC.delegate = self
Add following code
self.logoutVC = [[LogoutViewController alloc]init];
Everything will start working automatically.

I need to pass a string from a NSObject class to a UIViewController with a delegate

I need to pass a string from a NSObject class to a UIViewController, I understand that the best way is delegation but the delegate method isn't being called. I'm trying to set the UILabel an DieFacesViewController as the selectedOption from TemporarySelection.
A tableview shows the value of CustomOptionStore, once it's tapped passes its value to TemporarySelection and opens the modal view DieFacesViewCountroller which should, at least in my mind, take the label value from TemporarySelection. The reason I created TemporarySelection is because the DieFacesViewController will be used by other classes, not only by CustomOptionStore, and it will need to load the label from all those classes when different tableViews are selected.
I tried to set the delegate as self in both viewDidLoad and viewWillAppear with no luck, I don't understand if the view loads before being able to call the delegate method or if there's something wrong the way I set the method up.
I've been stuck here for two days, this is the first time I post a question so please forgive me if it's a bit confused.
my delegator class TemporarySelection.h is
#import <Foundation/Foundation.h>
#import "CustomOptionsStore.h"
#class DieFacesViewController;
#protocol TemporarySelectionDelegate <NSObject>
-(void)sendSelection;
#end
#interface TemporarySelection : NSObject
#property (nonatomic, weak) id <TemporarySelectionDelegate> delegate;
#property (nonatomic, strong) NSString *selectedOption;
-(void)addSelection: (CustomOptionsStore *) selection;
#end
and my TemporarySelection.m is
#import "TemporarySelection.h"
#implementation TemporarySelection
-(void)addSelection: (CustomOptionsStore *) selection{
self.selectedOption = selection.description;
[self.delegate sendSelection];
}
#end
the delegate class DiewFacesViewController.h is
#import <UIKit/UIKit.h>
#import "SelectedStore.h"
#import "TemporarySelection.h"
#interface DieFacesViewController : UIViewController <TemporarySelectionDelegate>
#property (strong, nonatomic) IBOutlet UILabel *SelectionName;
#end
and the DieFacesViewController.m is
#import "DieFacesViewController.h"
#interface DieFacesViewController ()
#end
#implementation DieFacesViewController
- (void)viewDidLoad {
TemporarySelection *ts = [[TemporarySelection alloc]init];
ts.delegate = self;
[super viewDidLoad];
}
-(void)sendSelection{
TemporarySelection *ts = [[TemporarySelection alloc]init];
self.SelectionName.text = ts.selectedOption;
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
}
You are not setting the delegate object properly.Check the above code
#import "DieFacesViewController.h"
#interface DieFacesViewController ()<TemporarySelectionDelegate>
{
//global object
TemporarySelection *ts;
}
#end
#implementation DieFacesViewController
- (void)viewDidLoad {
ts = [[TemporarySelection alloc]init];
ts.delegate = self;
[super viewDidLoad];
}
-(void)sendSelection{
//Use the object to extract
self.SelectionName.text = ts.selectedOption;
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
}

Custom delegation to pass data between tab bar view in iOS not working

I am facing a problem related to delegation.I have 2 tabView. In first tabview i have 2 textfield and a button(to trigger the delegate method) and in the second tabview i have 2 label to display the content of textfield in the first tabview.Whats wrong with my code???
For first tabviewA the .h file
#import <UIKit/UIKit.h>
#class ViewControllerA;
#protocol ViewControllerADelegate <NSObject>
-(void)sayHello:(ViewControllerA*)viewController;
#end
#interface ViewControllerA : UIViewController<UITextFieldDelegate>
#property (nonatomic,strong)IBOutlet UITextField *textFieldFirst;
#property (nonatomic,strong)IBOutlet UITextField *textFieldSecond;
#property (nonatomic,strong)id<ViewControllerADelegate>delegate;
-(IBAction)next:(id)sender;
#end
and the .m file
#import "ViewControllerA.h"
#interface ViewControllerA ()
#end
#implementation ViewControllerA
#synthesize textFieldFirst=_textFieldFirst;
#synthesize textFieldSecond=_textFieldSecond;
#synthesize delegate=_delegate;
- (void)viewDidLoad
{
[super viewDidLoad];
_textFieldFirst.delegate=self;
_textFieldSecond.delegate=self;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return NO;
}
-(IBAction)next:(id)sender
{
[self.delegate sayHello:self];
}
#end
And for the second tabbarViewB the .h file
#import <UIKit/UIKit.h>
#import "ViewControllerA.h"
#interface ViewControllerB : UIViewController<ViewControllerADelegate>
#property (nonatomic,strong)IBOutlet UILabel *labelFirst;
#property (nonatomic,strong)IBOutlet UILabel *labelSecond;
#end
and the .m file
#import "ViewControllerB.h"
#interface ViewControllerB ()
#end
#implementation ViewControllerB
#synthesize labelFirst=_labelFirst;
#synthesize labelSecond=_labelSecond;
- (void)viewDidLoad
{
[super viewDidLoad];
ViewControllerA *viewControllerA=[self.tabBarController.viewControllers objectAtIndex:0];
viewControllerA.delegate=self;
// Do any additional setup after loading the view.
}
-(void)sayHello:(ViewControllerA *)viewController
{
_labelFirst.text=viewController.textFieldFirst.text;
_labelSecond.text=viewController.textFieldSecond.text;
}
#end
N.B: I have tried tabView B to tabview A with the same process and that worked fine. The reverse (i.e from A to B)is not working at all.Thanks
ViewControllerB sets itself as the delegate in its viewDidLoad method. This method is called as soon as the View Controller has loaded the view it manages (into its view property). That view is loaded only when someone tries to access the view controller's view property for the first time. See here. There's a good chance that ViewControllerB's view has not been loaded yet, so the viewDidLoad method has not been called yet.
If you override the awakeFromNib method like so:
(void) awakeFromNib {
[super awakeFromNib];
ViewControllerA *viewControllerA=[self.tabBarController.viewControllers
objectAtIndex:0];
viewControllerA.delegate = self;
}
it should work, since that method will be called when the view controller is initialized.

iOS: Changing UIButton Title through Delegate

I'm using delegation to change the title of a UIButton.
.h MainView
//MainViewController.h
#import <UIKit/UIKit.h>
#class SignUpDelegate;
#protocol SignUpDelegate <NSObject>
#required
-(void)loggedIn;
#end
#interface MainViewController : UITableViewController <NSFetchedResultsControllerDelegate>
{
id <SignUpDelegate> delegate;
}
#property (nonatomic, assign) id <SignUpDelegate> delegate;
-(void)loggedIn;
#end
.m
#interface MainViewController ()
//This button is connected to the UINavigationBar Button that needs its title changed.
//Via Interface Builder, the default value of the title is setup as "Login"
-#property (weak, nonatomic) IBOutlet UIBarButtonItem *logInOutButton;
#end
-(void)loggedIn
{
NSLog (#"This is Logged in inside MainView.m");
self.logInOutButton.title = #"Logout";
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UIViewController *destinationViewController = segue.destinationViewController;
Signup *signUp = [destinationViewController isKindOfClass:[Signup class]] ? (Signup*)destinationViewController : nil;
signUp.mainViewController = self.delegate;
}
.h SignUp
#import <UIKit/UIKit.h>
#import "MainViewController.h"
#interface SignUp : UIViewController <UITextFieldDelegate, UIActionSheetDelegate, SignUpDelegate>
#property (strong, nonatomic) MainViewController *mainViewController;
#end
.m
#synthesize mainViewController;
- (IBAction)createUser:(id)sender
{
[self loggedIn];
}
- (void) loggedIn
{
NSLog (#"This is Logged in inside SignUp");
[mainViewController loggedIn];
}
So, both NSLogs Print fine, which I think means the delegate is working, however, the title on the UIButton on the Navigation Bar never changes to "Logout"
That's because you recreate STMasterViewController (should this have been MainViewController instead?) every time in the loggedIn delegate method. (You can verify this by adding a breakpoint on -[MainViewController loggedIn] and checking if self.logInOutButton is non-nil). Instead you should get the reference to existing instance of MainViewController and operate on that.

Resources