I have a parent view controller with a single method I want available to all child classes:
#import "GAViewController.h"
#interface GAViewController ()<UITextFieldDelegate>
#end
#implementation GAViewController
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
#end
I have a register view controller which looks like this:
//.h
#import <UIKit/UIKit.h>
#import "GAViewController.h"
#import "GAViewController.m"
#interface GARegisterViewController : GAViewController
#end
//.m
#import "GARegisterViewController.h"
#interface GARegisterViewController ()<UIActionSheetDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIGestureRecognizerDelegate>
#property (weak, nonatomic) IBOutlet UIButton *registerButton;
#property (weak, nonatomic) IBOutlet UITextField *userNameTextField;
#property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
#property (weak, nonatomic) IBOutlet UITextField *firstNameTextField;
#property (weak, nonatomic) IBOutlet UITextField *lastNameTextField;
#property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextField;
#property (weak, nonatomic) IBOutlet UISegmentedControl *genderSegmentedContoller;
#end
#implementation GARegisterViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self makeTextFieldDelegates];
}
- (void)makeTextFieldDelegates{
[self.userNameTextField setDelegate:self];
[self.passwordTextField setDelegate:self];
[self.firstNameTextField setDelegate:self];
[self.lastNameTextField setDelegate:self];
[self.phoneNumberTextField setDelegate:self];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Here is the error I receive:
Does anyone know I can either fix the error above? Or create a parent class correctly with the textFieldShoudlReturn method so that I don't have to include in all my views.
Thanks!
I am importing .m because it contains the textfieldshouldreturn method, it also imports the .h file
It is importing .m file that causes duplicate symbols. Importing a .m file causes the file from which it is imported to define the same symbols (such as method implementations and functions / variables with external scope) as the .m file being included, causing the duplication.
For the same reason one should never place #implementation blocks into header files.
In order to fix this, make a header for GAViewController, and declare textFieldShouldReturn: inside it. Remove #import "GAViewController.m" from your headers and .m files, replacing with #import "GAViewController.h". This should fix the problem.
Related
i have first display start_game screen than after when i click button that time display popupview using xib.in xib class i have create delegate method.when i close the popupview that time call delegate method but not calling
here is my delegate class
.h File
#import
#protocol digbuttonalertdelegate;
#interface digbuttonalert : UIViewController
#property (weak, nonatomic) IBOutlet UIImageView *bg_image;
#property (weak, nonatomic) IBOutlet UILabel *lbl_title;
#property (nonatomic, weak) id<digbuttonalertdelegate> delegate;
#end
#protocol digbuttonalertdelegate <NSObject>
#optional
-(void)digalertclose;
#end
.m File
#import "digbuttonalert.h"
#import "suggestion_alert.h"
#import "UIViewController+CWPopup.h"
#import "zoom_alert.h"
#interface digbuttonalert ()
{
bool status;
}
#end
#implementation digbuttonalert
- (void)viewDidLoad {
[super viewDidLoad];
status=0;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected)];
singleTap.numberOfTapsRequired = 1;
[self.bg_image setUserInteractionEnabled:YES];
[self.bg_image addGestureRecognizer:singleTap];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)close:(id)sender {
}
-(void)tapDetected{
NSLog(#"single Tap on imageview");
if(status==0)
{
self.lbl_title.text=#"As you walk you will discover the hidden map.This circle will show your progress.";
status=1;
}
else
{
[self dismissViewControllerAnimated:YES completion:nil];
if ([self.delegate respondsToSelector:#selector(digalertclose)]) {
[self.delegate digalertclose];
}
}
}
here this class i want to call method
#import "digbuttonalert.h"
#interface start_games () <MJSecondPopupDelegate,digbuttonalertdelegate>
{
- (void)viewDidLoad {
digbuttonalert *next=[[digbuttonalert alloc]init];
next.delegate=self;
next.modalTransitionStyle=UIWebPaginationModeRightToLeft;
next.modalPresentationStyle=17;
[self presentViewController:next animated:YES completion:nil];
}
- (void)digalertclose
{
[self StartTimer];
[[NSUserDefaults standardUserDefaults]setObject:#"false" forKey:#"alertstatus"];
}
Change .m file with below :
#protocol digbuttonalertdelegate <NSObject>
#optional
-(void)digalertclose;
#end
#interface digbuttonalert : UIViewController
#property (weak, nonatomic) IBOutlet UIImageView *bg_image;
#property (weak, nonatomic) IBOutlet UILabel *lbl_title;
#property (nonatomic, weak) id<digbuttonalertdelegate> delegate;
#end
And follow basic tutorial steps : enter link description here
Please implement digalertclose delegate in start_games controller like this
-(void)digalertclose {
}
You make digalertclose optional thats why crash does not occur if you remove optional keyword from digbuttonalertdelegate protocol you can see that there will be a crash because you try to fire a delegate but does not implement it.
In your digbuttonalert.h protocol is defined twice.
There is no #end for the interface.
Should be something like
ClassA.h
#protocol YourProtocol <NSObject>
#optional
-(void)yourProtocolOptionalMethod
#end
#interface ClassA.h
#property (weak, nonatomic) id <YourProtocol> delegate;
#end
ClassA.m
-(void) someClassAMethod {
[self.delegate yourProtocolOptionalMethod];
}
ClassB.h
#import "ClassA.h"
#interface ClassB <YourProtocol>
#end
ClassB.m
-(void) someClassBMethod {
ClassA *classA = [ClassA alloc] init];
classA.delegate = self;
}
After setting the delegate now when you will call the delegate from ClassA then it will trigger the protocol implemented method in ClassB
-(void)yourProtocolOptionalMethod {
NSlog(#"");
}
On your class , you need to declare the delegate like this:
#interface YourViewController () < digbuttonalertdelegate >
I am new to xcode so I was following a tutorial about putting a webpage into an ios objective-c project in xcode 7. Below is the code I use.
#interface ViewController ()
View controller header
#interface ViewController : UIViewController
IBOutlet UIWebView* webView;
#property (nonatomic, retain) UIWebView *webView;
#end
view controller main
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#synthesize webView;
#end
The bug warnings are;
1.Cannot declare variable inside #interface or #protocol'
2.Attribute can only be applied to instance variables or properties
And both apply to the first line starting with IBOutlet
for instance variables Just missing {}
#interface ViewController : UIViewController
{
IBOutlet UIWebView* webView;
}
#property (nonatomic, retain) UIWebView *webView;
#end
but actually the declaration can be skipped
eg.
#interface ViewController : UIViewController
#property (nonatomic, retain) IBOutlet UIWebView *webView;
#end
This line
#synthesize webView;
should put below #implementation for consistency, and can skip as well
(after Xcode 4.4 the compiler will generate for u, but it's good to know the original code)
more about class:
https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/ClassMethod.html
I am having trouble getting the keyboard to disappear after entering text. I have many solutions for previous versions of Xcode, but nothing for Xcode 7. My current ViewController.h file looks like:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITextFieldDelegate>
#property (strong, nonatomic) IBOutlet UITextField *txtUsername;
#property (strong, nonatomic) IBOutlet UITextField *txtPassword;
#end
My .m file looks like:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize txtUsername;
#synthesize txtPassword;
- (void)viewDidLoad {
[super viewDidLoad];
self.txtUsername.delegate = self;
self.txtPassword.delegate = self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (BOOL) textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
#end
I have assigned the delegate from the textField to the ViewController as well.
Update The View I was working with was not assigned to the ViewController class and therefore did not inherit the textFielfShouldReturn method I needed.
Try:
[self.view endEditing:YES];
I have created delegate method and working in Xcode lower version but not working in Xcode 6.1.
Its showing error Cannot Find protocol declaration NSObject
Tried code:
.h file
#class ReportCell;
#protocol keyboardDelegate <NSObject>
#optional
- (BOOL)leaveKeyboard:(ReportCell *)cell ;
#end
#import <UIKit/UIKit.h>
#interface ReportCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UIImageView *imgReport;
#property (strong, nonatomic) IBOutlet UITextField *txtReport;
#property (strong, nonatomic) IBOutlet UIView *viewReport;
#property (nonatomic, assign) id <keyboardDelegate> delegate;
#end
.m file
#import "ReportCell.h"
#implementation ReportCell
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[_delegate leaveKeyboard:self];
[self.superview endEditing:YES];
[super touchesBegan:touches withEvent:event];
}
Hey You can solve you problem by following change in your .h file.
#import <UIKit/UIKit.h>
#class ReportCell;
#protocol keyboardDelegate;
#interface ReportCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UIImageView *imgReport;
#property (strong, nonatomic) IBOutlet UITextField *txtReport;
#property (strong, nonatomic) IBOutlet UIView *viewReport;
#property (nonatomic, assign) id <keyboardDelegate> delegate;
#end
#protocol keyboardDelegate <NSObject>
#optional
- (BOOL)leaveKeyboard:(ReportCell *)cell ;
#end
I'm making elevator thing. I'm having trouble sending data with different views using presentModalViewController. I got red message "favoriteColorString" property not found. I copied exactly the same but different form names and buttons. The "favoriteColorString" appears an error and unable to send elevator2 data.
I tried two different thing.
Elevator2View.favoriteColorString = [[NSString alloc] initWithFormat:#"Your favorite color is %#", favoriteColorTextField.text];
And
favoriteColorString = [[NSString alloc] initWithFormat:#"Your favorite color is %#", favoriteColorTextField.text];
Here's my code:
ElevatorView.h
#import <UIKit/UIKit.h>
#import "Elevator2View.h"
#interface ElevatorView : UIViewController<PassSecondColor>
{
Elevator2View *Elevator2View;
IBOutlet UITextField *favoriteColorTextField;
IBOutlet UILabel *favoriteColorLabel;
IBOutlet UILabel *secondFavoriteColorLabel;
NSString *secondFavoriteColorString;
}
#property (nonatomic, retain) Elevator2View *Elevator2View;
#property (nonatomic, retain) IBOutlet UITextField *favoriteColorTextField;
#property (nonatomic, retain) IBOutlet UILabel *favoriteColorLabel;
#property (nonatomic, retain) IBOutlet UILabel *secondFavoriteColorLabel;
#property (copy) NSString *secondFavoriteColorString;
#end
ElevatorView.m
#import "ElevatorView.h"
#import "Elevator2View.h"
#implementation ElevatorView
#synthesize Elevator2View, favoriteColorTextField, favoriteColorLabel, secondFavoriteColorLabel;
#synthesize secondFavoriteColorString;
-(IBAction)level1:(id)sender;{
favoriteColorTextField.text = #"1";
Elevator2View.favoriteColorString = [[NSString alloc] initWithFormat:#"Your favorite color is %#", favoriteColorTextField.text];
[self presentModalViewController:[[[Elevator2View alloc] init]
autorelease] animated:NO];
}
Elevator2View.h
#import <UIKit/UIKit.h>
#protocol PassSecondColor <NSObject>
#required
- (void) setSecondFavoriteColor:(NSString *)secondFavoriteColor;
#end
#interface Elevator2View : UIViewController{
IBOutlet UITextField *secondFavoriteColorTextField;
IBOutlet UILabel *favoriteColorLabel;
IBOutlet UILabel *secondFavoriteColorLabel;
NSString *favoriteColorString;
id <PassSecondColor> delegate;
}
#property (copy) NSString *favoriteColorString;
#property (nonatomic, retain) IBOutlet UITextField *secondFavoriteColorTextField;
#property (nonatomic, retain) IBOutlet UILabel *favoriteColorLabel;
#property (nonatomic, retain) IBOutlet UILabel *secondFavoriteColorLabel;
#property (retain) id delegate;
#end
Elevator2View.m
#import "Elevator2View.h"
#interface Elevator2View ()
#end
#implementation Elevator2View
#synthesize secondFavoriteColorTextField, favoriteColorLabel, secondFavoriteColorLabel;
#synthesize favoriteColorString;
#synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void) viewWillAppear:(BOOL)animated
{
favoriteColorLabel.text = favoriteColorString;
}
- (void) viewWillDisappear:(BOOL) animated
{
// [[self delegate] setSecondFavoriteColor:secondFavoriteColorTextField.text];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
favoriteColorLabel.text = favoriteColorString;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
See http://www.theappcodeblog.com/?p=90
The reason for your "property not found" is that you named your ivar same as class.
Dot notation is just a syntactic sugar: object.property = value is equivalent to [object setProperty:value]. In Objective C classes are also objects, and when you call Elevator2View.favoriteColorString = whatever, Xcode apparently thinks that you are attempting to call class method setFavoriteColorString of class Elevator2View.
Getting rid of this error is easy: just rename your ivar Elevator2View *Elevator2View to something else. In fact, Xcode 4.4 and newer autosynthesizes ivars for your properties: if you have a property propertyName, then Xcode will autosynthesize ivar _propertyName. Your property Elevator2View will have _Elevator2View ivar. So unless you really really need to have ivars with different naming scheme, you can get rid of your #synthesize, and you also don't need to declare ivars for you properties.
(Though I prefer to declare ivars for properties (following Xcode naming scheme), because far too often lldb doesn't show autosynthesized-without-declaring ivars in object inspector.)
That was about properties, ivars and naming conventions. But what are you doing in this code?
-(IBAction)level1:(id)sender;{
favoriteColorTextField.text = #"1";
Elevator2View.favoriteColorString = [[NSString alloc] initWithFormat:#"Your favorite color is %#", favoriteColorTextField.text];
[self presentModalViewController:[[[Elevator2View alloc] init]
autorelease] animated:NO];
}
You set value of Elevator2View's - your instance variable's - property, then create brand new object of Elevator2View class and present that as modal view controller. (By the way, presentModalViewController:animated: is deprecated in iOS 6.0). Of course, this brand new Elevator2View object has no idea what Elevator2View's (your instance variable's) properties are!