ViewController UIwebview issue - ios

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

Related

Linking two UITextView to scroll together if one is scrolled

I have two text views beside each other with multiple lines of text.
How can I make them both scroll together based on scrolling either one of them at the same time?
First
#interface ViewController : UIViewController <UITextViewDelegate>
second make both textViews delagates
tv1.delegate = self
tv2.delegate = self
Implement
-(void)scrollViewDidScroll:(UIScrollView *)inScrollView {
self.tv1.contentOffset = inScrollView.contentOffset;
self.tv2.contentOffset = inScrollView.contentOffset;
}
Here is a demo txView
Edit
.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITextViewDelegate>
#property (weak, nonatomic) IBOutlet UITextView *tv1;
#property (weak, nonatomic) IBOutlet UITextView *tv2;
#end
.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tv1.delegate = self;
self.tv2.delegate = self;
}
- (void)scrollViewDidScroll:(UIScrollView *)inScrollView {
self.tv1.contentOffset = inScrollView.contentOffset;
self.tv2.contentOffset = inScrollView.contentOffset;
}

How to make the keyboard disappear programmatically in iOS

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];

Unknown type name error in Xcode 6

The goal of my app is to calculate two fields that will return in a text box. Unfortunately I'm receiving an error regarding with unknown type field referring to my TotalField.text field. I have two views. One is the home screen which refers to the second view through a button and the second view contains these fields and labels that I'm trying to target.
This is my code.
ViewController.h
// SalaryCalcApp
//
//
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *Total;
#property (strong, nonatomic) IBOutlet UILabel *HourlyRate;
#property (strong, nonatomic) IBOutlet UILabel *NumberOfHours;
#property (nonatomic, strong) UITextField *TotalField;
#property (nonatomic, strong) UITextField *HourlyRateField;
#property (nonatomic, strong) UITextField *NumberOfHoursField;
#end
ViewController.m
// SalaryCalcApp
//
//
//
#import "ViewController.h"
#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 TotalField;
#synthesize Total;
#synthesize HourlyRate;
#synthesize NumberOfHours;
#synthesize HourlyRateField;
#synthesize NumberOfHoursField;
TotalField.text = [ NSString stringWthFormat: #"%i", (HourlyRateField.text.intValue * NumberOfHoursField.text.intValue) ];
#end
The error resides in this code:
TotalField.text = [ NSString stringWthFormat: #"%i", (HourlyRateField.text.intValue * NumberOfHoursField.text.intValue)];
Error Message:
Unknown type name 'TotalField' and Expected identifier or '('
Thank you everyone for your contributions.
Why is that code not nested inside a function?
Put it inside your viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
TotalField.text = [ NSString stringWthFormat: #"%i", (HourlyRateField.text.intValue * NumberOfHoursField.text.intValue) ];
}

Property not found although it's in the header

I have one view and one View Controller. I am trying to have a reference to View Controller from the view.
PlusCalendarView.h
#interface PlusCalendarView : TSQCalendarView
#property (nonatomic, strong) UIViewController *initialVC;
#end
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpCalendarView:self.myCalendarView];
self.myCalendarView.initialVC = self; // ERROR: Property initialVC not found on object of type "PlusCalendarView"
}
How come it cannot find initialVC property?
Update:
ViewController.h
#import <UIKit/UIKit.h>
#import <TimesSquare/TimesSquare.h>
#interface PlusCalendarView : TSQCalendarView;
#end
#interface ViewController : UIViewController
#property (nonatomic, strong) NSCalendar *calendar;
#property (strong, nonatomic) IBOutlet PlusCalendarView *myCalendarView;
#end
The problem is that you're declaring PlusCalendarView in ViewController.h AND PlusCalendarView.h. You should remove the #interface declaration from ViewController.h.
Instead, add #class PlusCalendarView in ViewController.h then #import "PlusCalendarView.h" in ViewController.m

ios uiviewcontroller inheritance throws duplicate symbols error

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.

Resources