save method and delegate in detailTableViewController fail in Xcode - ios

I have a bug when I save information passed by a user in a detailTableViewController. Though when I force the debugger by pressing lecture, it carries on performing the coredata job, etc.
Here is my structure:
A detailTableViewController which presents staticcells that the user fills and then passed to the coredata.
It implements a delegate for the MainTableViewController to pop over it.
PhraseDetailTableViewController.h:
#import <UIKit/UIKit.h>
#import "Phrase.h"
#class PhraseDetailTableViewController;
#protocol AddPhraseTVCDelegate
- (void) theSaveButtonOnThePhraseDetailWasTapped:(PhraseDetailTableViewController *) controller;
#end
#interface PhraseDetailTableViewController : UITableViewController
#property (strong,nonatomic) NSManagedObjectContext *managedObjectContext;
#property (nonatomic,weak) id <AddPhraseTVCDelegate> delegate;
#property (strong, nonatomic) IBOutlet UITextField *phraseMotivationTextField;
- (IBAction)save:(id)sender;
#end
PhraseDetailTableViewController.m :
- (void)save:(id)sender
{
NSLog(#"Telling the PhraseDetailTableViewController Delegate that Save was tapped on the PhraseDetailTableViewController");
Phrase *phrase = [NSEntityDescription insertNewObjectForEntityForName:#"Phrase" inManagedObjectContext:self.managedObjectContext];
phrase.motivation = phraseMotivationTextField.text;
NSLog(#"la phrase est %#", phrase.motivation);
[self.managedObjectContext save:nil];
[self.delegate theSaveButtonOnThePhraseDetailWasTapped:self];
NSLog(#"le message savebuttononethe.. a ete envoyé");
}
I delegated it to the main, here is the .H and .m
PhraseTableViewController.h :
#import <UIKit/UIKit.h>
#import "PhraseDetailTableViewController.h"
#import "CoreDataTableViewController.h"
#import "Phrase.h"
#interface PhraseTableViewController : CoreDataTableViewController <AddPhraseTVCDelegate>
#property (strong,nonatomic) NSFetchedResultsController *fetchedResultsController;
#property (strong,nonatomic) NSManagedObjectContext *managedObjectContext;
#end
PhraseTableViewController.m (extract where i pass the delegate and It receive the delegate method):
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"Add Phrase Segue"])
{
NSLog(#"Seeting PhraseTVC as a delegate of PhraseDetailTableViewController");
PhraseDetailTableViewController *phraseDetailTableViewController= segue.destinationViewController;
phraseDetailTableViewController.delegate=self;
phraseDetailTableViewController.managedObjectContext=self.managedObjectContext;
}
}
- (void)theSaveButtonOnThePhraseDetailWasTapped:(PhraseDetailTableViewController *)controller
{
[controller.navigationController popViewControllerAnimated:YES];
NSLog(#"c'est la methode en elle meme qui chie");
}
I think there is something to do with the subclassing of the Main/detail and its inherited delegate. But i can't figure out what exactly implies this bug :s
CHeers,
Louis

Related

iOS: how class notify something to UI view?

Starting point
I have a class, say A, used by an UI view.
A has a delegate that should notify UI view and this one should be write something on screen.
Question
What is the best approach to achieve this feature?
Seems something like observer-observable pattern
Code
---A.h
#interface A : NSObject
#end
---A.m
#implementation A
-(void)fooDelegate:(FooType *)sender {
/* Here I need to notify UI (that change notificationArea.text) */
}
---UIView.h
#interface UIView : UIViewController
#property(strong, nonatomic, retain) A* a;
#property(strong, nonatomic) IBOutlet UITextField *notificationArea;
#end
Based on the comments, I guess just code is what you're looking for...
Create your delegate protocol:
#protocol ADelegate;
#interface A : NSObject
#property (nonatomic, weak) id <ADelegate> delegate;
#end
#protocol ADelegate <NSObject>
#optional
-(void)fooDelegate:(A *)a;
#end
Notify your delegate:
#implementation A
-(void)fooDelegate:(FooType *)sender {
if ([[self delegate] respondsToSelector:#selector(fooDelegate:)]) {
[[self delegate] fooDelegate:self];
}
}
#end
Conform to the delegate protocol:
#import "A.h"
#import "MyView.h"
#interface MyView <ADelegate>
#end
#implementation MyView
-(void)fooDelegate:(A *)a {
// update text field here
}
#end
Finally, whenever you create an instance of A, set the delegate (where self in this example is an instance of MyView:
A *a = [[A alloc] init];
[a setDelegate:self];

Pass value from controller to controller

I'm building my first iOS app to try and learn iOS coding. However, I need to pass a value xAuthToken from one controller to the other. I can't seem to get it working to. I init the value in the first controller ViewController, but then I need it in SettingsController. I keep getting errors and the one it throws now is: Property 'ViewController' not found on object of type 'ViewController'
What am I doing wrong?
ViewController.h
#import <UIKit/UIKit.h>
#import "SettingsController.h"
#interface ViewController : UIViewController <UITextFieldDelegate>
#property (weak, nonatomic) IBOutlet UITextField *txtUsername;
#property (weak, nonatomic) IBOutlet UITextField *txtPassword;
- (IBAction)sigininClicked:(id)sender;
- (IBAction)backgroundTap:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#property (nonatomic) NSString *xAuthToken;
#end
#implementation ViewController
- (IBAction)sigininClicked:(id)sender {
NSString *xAuthToken = #"0";
self.viewController = [[SettingsController alloc] init];
self.viewController.xAuthToken = xAuthToken;
[self performSegueWithIdentifier:#"login_success" sender:self];
}
SettingsController.h
#import <Foundation/Foundation.h>
#interface SettingsController : UIViewController
#end
SettingsController.m
#interface SettingsController ()
#property (weak, nonatomic) IBOutlet UITextField *myTextField;
#property (weak, nonatomic) IBOutlet UISwitch *mySwitch;
#end
#implementation SettingsController
//THIS IS WHERE I NEED XAUTHTOKEN
If you have a segue that presents your SettingsController, pass the information it needs in prepareForSegue. Don't create a new local SettingsController.
You can do something like
viewController = [self.storyboard instantiateviewcontrollerwithidentifier:#"your vc name"];
viewController.Xauthtoken = data;
// Do something else with this.
Or another way ( I don't know if it's good or bad, use it while your vc is already active ):
Use NSNotificationCenter and pass values between it.
Third way is create delegate for first Viewcontroller.
Try this in your ViewController.m:
#import "ViewController.h"
#interface ViewController ()
#property (nonatomic) NSString *xAuthToken;
#end
#implementation ViewController
- (IBAction)sigininClicked:(id)sender {
xAuthToken = #"Your Value Here";
[self performSegueWithIdentifier:#"login_success" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"login_success"]) {
SettingsController *destViewController = segue.destinationViewController;
destViewController.xAuthToken = xAuthToken;
}
}
And in your SettingsController.h
#import <Foundation/Foundation.h>
#interface SettingsController : UIViewController
#property (nonatomic,strong) NSString *xAuthToken;
#end
SettingsController.m
#interface SettingsController ()
#property (weak, nonatomic) IBOutlet UITextField *myTextField;
#property (weak, nonatomic) IBOutlet UISwitch *mySwitch;
#end
#implementation SettingsController
-(void)viewDidLoad{
NSLog("%#",xAuthToken);
}

iOS Protocol Issues

I have been staring at code too long and know i am doing something silly here with my Protocols if someone could enlighten me that would be great.
Trying to get my areaNameLabel to change to cell.nameLabel.text across viewcontrollers.
FirstTableViewController.h
#import <UIKit/UIKit.h>
#import "FirstTableCell.h"
#import "SecondViewController.h"
#interface FirstTableViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate, passNames>
#property (nonatomic, strong) NSString *passedNameString;
#property (strong, nonatomic) NSMutableArray *names;
FirstTableViewController.m
#import "FirstTableViewController.h"
#interface FirstTableViewController ()
#end
#implementation FirstTableViewController
#synthesize names;
#synthesize passedNameString;
- (void)viewDidLoad
{
[super viewDidLoad];
names = [NSMutableArray arrayWithObjects:#"Bondi", #"Miranda", nil];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
FirstTableCell *cell = (FirstTableCell *)[tableView cellForRowAtIndexPath:indexPath];
if ([cell.nameLabel.text isEqualToString:#"Bondi"]) {
SecondViewController *mapController = [[SecondViewController alloc] init];
NSString *passedName = cell.nameLabel.text;
mapController.passedNameString = passedName;
[mapController setDelegate:self];
self.tabBarController.selectedIndex = 1;
NSLog(#"Hola");
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark - Protocol Methods
-(void)setAreaName:(NSString *)areaName {
passedNameString = areaName;
}
SecondViewController.h
#import <UIKit/UIKit.h>
#protocol passNames <NSObject>
-(void)setAreaName:(NSString *)areaName;
#end
#interface SecondViewController : UIViewController <RMMapViewDelegate>
#property (retain) id <passNames> delegate;
#property (nonatomic, strong) NSString *passedNameString;
#property (weak, nonatomic) IBOutlet RMMapView *mapView;
#property (weak, nonatomic) IBOutlet UILabel *areaNameLabel;
#end
SecondViewController.m
#import "SecondViewController.h"
#import "FirstTableViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController #synthesize areaNameLabel; #synthesize delegate, passedNameString;
- (void)viewDidLoad {
[super viewDidLoad];
passedNameString = areaNameLabel.text;
[[self delegate] setAreaName:passedNameString];
if ([areaNameLabel.text isEqualToString:#"Bondi"]) {
NSLog(#"You got it!");
}
}
Any other critiques feel free to throw in - I've had a look at some other Protocol questions and examples but i know it is something obvious i am missing.
The problem is that your SecondViewController has no relationship to the passNames protocol (being declared in the same header does not count).
Since protocol methods need to be implemented (or their implementation be inherited from the base) and your SecondViewController does not do that, you cannot call setAreaName: without triggering an error.
If you would like to use a common protocol in two view controllers, you need to do this:
Give passNames protocol a more conventional name that starts in a capital letter, and put it in a separate header file
Include that header in both view controllers (the #import "SecondViewController.h" in the FirstTableViewController.h does not look right)
Put implementations of setAreaName: in both view controllers.
Note that you cannot put the common functionality in a superclass, because your view controllers inherit from different bases (i.e. UIViewController and UITableViewController).

'NSInvalidArgumentException', reason: '-[UINavigationController setMed:]: unrecognized selector sent to instance 0x746dda0'

I am making an app that will help people with certain health conditions manage their medication. I have created a modal to add medication which works and saves the new medication using core data.
I am now trying to allow people to edit their medication after it has been saved. To do this I am trying to send a managed object of the medication to a "fibromappMedsEditViewController" and assign the information in the viewDidLoad method of the class.
I keep getting this error:
'NSInvalidArgumentException', reason: '-[UINavigationController setMed:]: unrecognized selector sent to instance 0x746dda0'
Could anybody tell me what I am doing wrong?
relevant methods in fibromappMedsListViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ //selMed declared at top of file as NSManagedObject *selMed;
selMed = [self.meds objectAtIndex:indexPath.row];
NSLog(#"SELECTED MED: %#",[selMed valueForKey:#"name"] );
UIStoryboardSegue *segueString = [NSString stringWithFormat:#"%#",#"editMeds"];
NSLog(#"%#",segueString);
[self performSegueWithIdentifier:#"editMeds" sender:indexPath];
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSLog(#"%#",segue.destinationViewController);
NSLog(#"%#",[selMed valueForKey:#"name"] );
fibroMappMedsEditViewController *dest = segue.destinationViewController;
dest.med = selMed;
}
The fibroMappMedsEditViewController.h
#import <UIKit/UIKit.h>
#interface fibroMappMedsEditViewController : UITableViewController
- (IBAction)saveChanges:(id)sender;
- (IBAction)deleteBtnPressed:(id)sender;
- (IBAction)dosageChanged:(id)sender;
- (IBAction)maxDosageChanged:(id)sender;
- (IBAction)cancel:(id)sender;
- (IBAction)typeChanged:(id)sender;
#property (weak, nonatomic) IBOutlet UITextField *tbName;
#property (weak, nonatomic) IBOutlet UITextField *tbDose;
#property (weak, nonatomic) IBOutlet UITextField *tbMaxDose;
#property (weak, nonatomic) IBOutlet UITextField *tbType;
#property (weak, nonatomic) IBOutlet UIStepper *stepperDose;
#property (weak, nonatomic) IBOutlet UIStepper *stepperMaxDose;
#property (weak, nonatomic) IBOutlet UISegmentedControl *changeMeasure;
#property (strong, nonatomic) NSManagedObject *med;
#end
The fibroMappMedsEditViewController.m - just the parts I altered that affect the way the controller loads
#import "fibroMappMedsEditViewController.h"
#import "fibroMappAppDelegate.h"
#import <CoreData/CoreData.h>
#interface fibroMappMedsEditViewController ()
#end
#implementation fibroMappMedsEditViewController
#synthesize tbName;
#synthesize tbDose;
#synthesize tbMaxDose;
#synthesize tbType;
#synthesize med;
double dose;
double maxDose;
- (void)viewDidLoad
{
[super viewDidLoad];
tbName.text = [med valueForKey:#"name"];//name is a string in the model
tbDose.text = [med valueForKey:#"dose"];//dose is a double in the model
tbMaxDose.text = [med valueForKey:#"maxDose"];//maxDose is a double in the model
tbType.text = [med valueForKey:#"type"];//type is a string in the model
}
If you need to see anything else please just ask.
Also, I am using storyboards for this app.
It appears from that log, that your fibroMappMedsEditViewController (which should start with a capital letter BTW) is embedded in a navigation controller. You need to get to that navigation controller's root view controller.
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
UINavigationController *nav = segue.destinationViewController;
fibroMappMedsEditViewController *dest = (fibroMappMedsEditViewController *)nav.topViewController;
dest.med = selMed;
}

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