Table view controller with static cells - ios

i have a problem with my application,so when i put this code in InfoViewController and i run the application,the view appears to be empty.I have a table view setted to static cells,3 sections,style grouped with header for every section,the first cell contain a text field that i have used as a search field with google,and a search button,the second and the third contains a label.I have connected all the text field button and labels in the storyboard file,and the delegate in file’s owner.Should I Implement any other method?
Thanks for the advice.
#import <UIKit/UIKit.h>
#interface InfoViewController : UIViewController <UINavigationControllerDelegate>
#property (weak, nonatomic) IBOutlet UITextField *googleSearchTextField;
#property (weak, nonatomic) IBOutlet UIButton *searchButton;
#property (weak, nonatomic) IBOutlet UILabel *Label1;
#property (weak, nonatomic) IBOutlet UILabel *Label2;
-(IBAction)googleSearch;
#end
#import "InfoViewController.h"
#end
#implementation InfoViewController
#synthesize googleSearchTextField;
#synthesize searchButton;
#synthesize Label1;
#synthesize Label2;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.Label1.text = #“Label1”;
self.Label2.text = #“Label2”;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)textFieldShouldReturn:(UITextField*)textField
{
[textField resignFirstResponder];
return YES;
}
-(IBAction)googleSearch
{
NSString *searchString=googleSearchTextField.text;
NSString *urlAdress = [NSString stringWithFormat:#"http://www.google.com/search?q=%#",
searchString];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlAdress]];
}
#end

You need to add your tableViewDelegate methods and TableViewDataSource methods
Check https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/CreateConfigureTableView/CreateConfigureTableView.html#//apple_ref/doc/uid/TP40007451-CH6-SW10

Related

Objective c - pass back string value using delegate

I want to pass UITextField *nameText from GreenViewController to UILabel *nameValue in ViewController using delegate.
I made delegate, the onSave method in ViewController is called but the screen is not going back and the name is not passed.
What is the problem?
Here are the header and impelentation files:
GreenViewController.h
#import <UIKit/UIKit.h>
#protocol GreenViewDelegate <NSObject>
-(void)onSave:(NSString*)nameValue;
#end
#interface GreenViewController : UIViewController
#property id<GreenViewDelegate> delegate;
#property (nonatomic) NSString* sliderValuePassed;
#property (weak, nonatomic) IBOutlet UIButton *Next;
#property (weak, nonatomic) IBOutlet UIButton *Save;
#property (weak, nonatomic) IBOutlet UIButton *Back;
#property (weak, nonatomic) IBOutlet UILabel *sliderTextValue;
#property (weak, nonatomic) IBOutlet UITextField *NameText;
- (IBAction)save:(id)sender;
#end
GreenViewController.m
#import "GreenViewController.h"
#import "ViewController.h"
#interface GreenViewController ()
#end
#implementation GreenViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.sliderTextValue.text = self.sliderValuePassed;
}
- (IBAction)back:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)save:(id)sender {
[self.delegate onSave:self.NameText.text];
[self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#import "GreenViewController.h"
#interface ViewController : UIViewController <GreenViewDelegate>
#property (weak, nonatomic) IBOutlet UISlider *slider;
#property (weak, nonatomic) IBOutlet UILabel *sliderTextValue;
#property (weak, nonatomic) IBOutlet UIButton *EnterName;
#property (weak, nonatomic) IBOutlet UILabel *NameValue;
#property (weak, nonatomic) IBOutlet UIButton *LikeIOS;
#property (weak, nonatomic) IBOutlet UISwitch *CheckboxIOS;
#end
ViewController.m
#import "ViewController.h"
#import "RedViewController.h"
#import "GreenViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.sliderTextValue.text = [NSString stringWithFormat:#"Slider value = %d",(int)self.slider.value];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"yellowToGreen"]){
GreenViewController* gVC = segue.destinationViewController;
gVC.sliderValuePassed = [NSString stringWithFormat:#"Slider value = %d",(int)self.slider.value];
gVC.delegate = self;
}else if([segue.identifier isEqualToString:#"yellowToRed"]){
RedViewController* rVC = segue.destinationViewController;
rVC.sliderValuePassed = [NSString stringWithFormat:#"Slider value = %d",(int)self.slider.value];
rVC.CheckboxIOS = self.CheckboxIOS;
}
}
- (IBAction)sliderChangeValue:(id)sender {
int sliderVal = self.slider.value;
self.sliderTextValue.text = [NSString stringWithFormat:#"Slider value = %d",sliderVal];
}
-(void)onSave:(NSString*)nameValue{
NSLog(#"onSave in father controller");
self.NameValue.text = [NSString stringWithFormat:#"Name = %#",nameValue];
}
#end
I assume you didnt set the delegate .
Set it like this:
GreenViewController *myVc = [[GreenViewController alloc] init];
myVc.delegate = self;
Put this in your view controller view did load method!!!
try with GreenViewController.h:
#property (nonatomic, assign) id<GreenViewDelegate> delegate;
and GreenViewController.m:
- (IBAction)save:(id)sender {
[_delegate onSave:self.NameText.text];
[self.navigationController popViewControllerAnimated:YES];
}
ViewController.* seems to be OK

Keyboard not resigning when i tap the return key (xcode)

I am trying to get the keyboard to go away, when I press the return key on my application heres my code from the .m:
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
and the code in the .h:
- (BOOL)textFieldShouldReturn:(UITextField* )textField;
But when I run the application and hit the return key nothing happens. What am I doing wrong?
my .h:
#import <UIKit/UIKit.h>
#interface ChemicalInfoViewController: UIViewController{
IBOutlet UIScrollView *ChemicalInforScroller;
}
#property (strong, nonatomic) IBOutlet UITextField *PPMForChlTextField;
#property (strong, nonatomic) IBOutlet UITextField *GallonsForChlTextField;
#property (strong, nonatomic) IBOutlet UITextField *PPMForAlkTextField;
#property (strong, nonatomic) IBOutlet UITextField *GallonsForAlkTextField;
#property (strong, nonatomic) IBOutlet UITextField *PPMForPHTextField;
#property (strong, nonatomic) IBOutlet UITextField *GallonsForPHTextField;
- (IBAction)SumbitCDI:(UIButton *)sender;
#property (strong, nonatomic) IBOutlet UILabel *PPMLabelForChl;
#property (strong, nonatomic) IBOutlet UILabel *GallonsLabelForChl;
#property (strong, nonatomic) IBOutlet UILabel *PPMLabelForAlk;
#property (strong, nonatomic) IBOutlet UILabel *GallonsLabelForAlk;
#property (strong, nonatomic) IBOutlet UILabel *PPMLabelForPH;
#property (strong, nonatomic) IBOutlet UILabel *GallonsLabelForPH;
#property (strong, nonatomic) IBOutlet UITextField *ChlorinePoundsTextField;
#property (strong, nonatomic) IBOutlet UITextField *GallonsForAlkDecreaser;
#property (strong, nonatomic) IBOutlet UITextField *PhPoundsTextField;
#property (strong, nonatomic) IBOutlet UITextField *AlkPoundsTextField;
#property (strong, nonatomic) IBOutlet UITextField *LbsForAlkDecreaser;
#property (strong, nonatomic) IBOutlet UITextField *PPMForAlkDecreaser;
#property (strong, nonatomic) IBOutlet UITextField *LbsForPhDecreaser;
#property (strong, nonatomic) IBOutlet UITextField *PPMForPhDecreaser;
#property (strong, nonatomic) IBOutlet UITextField *GallonsForPhDecreaser;
- (IBAction) clickedBackground;
#interface ChemicalInfoViewController : UIView <UITextField>
#end
and my .m:
#import "ChemicalInfoViewController.h"
#interface ChemicalInfoViewController ()
#end
#implementation ChemicalInfoViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[ChemicalInforScroller setScrollEnabled:YES];
[ChemicalInforScroller setContentSize:CGSizeMake(320, 1000)];
self.textField.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction) clickedBackground {
[self.view endEditing:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
In your .h, remove:
- (BOOL)textFieldShouldReturn:(UITextField* )textField;
And it will work as it should, given that in your .h, you conform to the protocol:
#interface yourViewController : UIView <UITextFieldDelegate>
And set up the delegate like so:
yourTextField.delegate = self;
Edit regarding to the #end:
In your .m, add a #end to the bottom; so , it becomes (scroll to the very bottom):
#implementation ChemicalInfoViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[ChemicalInforScroller setScrollEnabled:YES];
[ChemicalInforScroller setContentSize:CGSizeMake(320, 1000)];
self.textField.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction) clickedBackground {
[self.view endEditing:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
#end //Here :-)
You didn't set the delegates for your textfields.
In "viewDidLoad" add
_ChlorinePoundsTextField.delegate = self;
_GallonsForAlkDecreaser.delegate = self;
_PhPoundsTextField.delegate = self;
_AlkPoundsTextField.delegate = self;
_LbsForAlkDecreaser.delegate = self;
_PPMForAlkDecreaser.delegate = self;
_LbsForPhDecreaser.delegate = self;
_PPMForPhDecreaser.delegate = self;
_GallonsForPhDecreaser.delegate = self;
none of these methods seemed to be working for some reason but what i did end up doing was creating a DidEndOnExit action and resigned the keyboard under that action for each textfield

Xcode thinks delegate method is actually a new method being declared

I just created a new utility application from the Xcode templates and I have a problem with the - (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller method not being called. When I use the three finger tap on the trackpad to define it, it says that method is defined in MainViewController.m when it is definitely in the protocol of Flip.
MainVC.h
#import "FlipsideViewController.h"
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate, UIPopoverControllerDelegate>
#property (strong, nonatomic) UIPopoverController *flipsidePopoverController;
#property (weak, nonatomic) IBOutlet UIView *backgroundView;
#end
.m
#import "MainViewController.h"
#interface MainViewController ()
#end
#implementation MainViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Background.jpg"]];
_backgroundView.contentMode = UIViewContentModeCenter;
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Flipside View Controller
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
//width
if (controller.widthSegmentedControl.selectedSegmentIndex == 0) {
//number of icons
} else {
//icon space
}
NSLog(#"b"); // not called
[self.flipsidePopoverController dismissPopoverAnimated:YES];
}
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
self.flipsidePopoverController = nil;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showAlternate"]) {
[[segue destinationViewController] setDelegate:self];
UIPopoverController *popoverController = [(UIStoryboardPopoverSegue *)segue popoverController];
self.flipsidePopoverController = popoverController;
self.flipsidePopoverController.delegate = self;
}
}
- (IBAction)togglePopover:(id)sender
{
if (self.flipsidePopoverController) {
[self.flipsidePopoverController dismissPopoverAnimated:YES];
self.flipsidePopoverController = nil;
} else {
[self performSegueWithIdentifier:#"showAlternate" sender:sender];
}
}
#end
FlipVC.h
#import <UIKit/UIKit.h>
#class FlipsideViewController;
#protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
#end
#interface FlipsideViewController : UIViewController
#property (weak, nonatomic) IBOutlet UISegmentedControl *widthSegmentedControl;
#property (weak, nonatomic) IBOutlet UITextField *widthNumberOfIcons;
#property (weak, nonatomic) IBOutlet UITextField *widthIconSpace;
#property (weak, nonatomic) IBOutlet UISegmentedControl *heightSegmentedControl;
#property (weak, nonatomic) IBOutlet UITextField *heightNumberOfIcons;
#property (weak, nonatomic) IBOutlet UITextField *heightIconSpace;
#property (weak, nonatomic) id <FlipsideViewControllerDelegate> delegate;
- (IBAction)done:(id)sender;
#end
.m
#import "FlipsideViewController.h"
#interface FlipsideViewController ()
#end
#implementation FlipsideViewController
- (void)awakeFromNib
{
self.preferredContentSize = CGSizeMake(320.0, 480.0);
[super awakeFromNib];
}
- (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.
}
#pragma mark - Actions
- (IBAction)done:(id)sender
{
NSLog(#"a"); //this is called
[self.delegate flipsideViewControllerDidFinish:self];
}
#end
There are two NSLogs in there, one for the method where the done button gets pressed (a) and one for the one where that didFinish method should be called (b). The problem is that when I press the done button on the Flip controller nothing happens (although "a" gets logged).
I found the problem. I removed the default button bar Info item and replace it with a normal button. The problem is that the segue needs to be anchored to that button and connected to the togglePopover method in the MainViewController.m file.

Make some calculation on Viewcontroller, need show the result to another Viewcontroller

I have an app, where I making some calculation, and need to transfer result of this calculation to ViewController3 and show the result there. Now I use label in same ViewController2 where I have calculation. Thank you for your help.
ViewController2.h
#import <UIKit/UIKit.h>
#interface ViewController2 : UIViewController<UITextFieldDelegate>
#property (weak, nonatomic) IBOutlet UILabel *gasPrice;
#property (weak, nonatomic) IBOutlet UILabel *gasCarMileage;
#property (weak, nonatomic) IBOutlet UITextField *perGalon;
#property (weak, nonatomic) IBOutlet UITextField *miles;
#property(nonatomic, copy, readonly) NSString *result;
- (IBAction)getIt:(id)sender;
#end
ViewController2.m
#import "ViewController2.h"
#import "ViewController3.h"
#interface ViewController2 ()
#end
#implementation ViewController2
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
//Start calculation
- (IBAction)getIt:(id)sender; {
float perGalon = ([_perGalon.text floatValue]);
float miles = ([_miles.text floatValue]);
float mileCost = perGalon / miles;
[self performSegueWithIdentifier:#"viewController3" sender: nil];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"viewController3"]) {
ViewController3 *viewController3 = [segue destinationViewController];
viewController3.result = [[NSString alloc] initWithFormat: #"Every mile you drive
will cost you $ %f", mileCost];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.perGalon.delegate = self;
self.miles.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
return [textField resignFirstResponder];
}
#end
ViewController3.h
#import <UIKit/UIKit.h>
#interface ViewController3 : UIViewController
#property(nonatomic, copy) NSString *result;
#end
ViewController3.m
#import "ViewController3.h"
#import "ViewController2.h"
#interface ViewController3 ()
#end
#interface ViewController2 ()
#property(nonatomic, copy, readwrite) NSString *result;
#end
#implementation ViewController3
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
You can define a string property in View controller 3 which you will set when you push view controller 3 from view controller 2.
ViewController3 interface declaration
#interface ViewController2 ()
#property(nonatomic, copy) NSString *result;
#end
In ViewController 2, you will implement this line at the last of your getIt() method.
[self performSegueWithIdentifier:#"viewController3" sender: nil]
And implement another method called prepareForSegue as follows
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"viewController3"]) {
ViewController3 *viewController3 = [segue destinationViewController];
viewController3.result = "YOUR CALCULATION RESULT HERE"
}
}
Bear in mind, you have to set identifier for view controller 3, go to storyboard and select the view for view controller 3 and On identify inspector, specified Storyboard ID as "viewController3".
Use a protocol,
In ViewController2.h
#protocol ViewController2Delegate;
#property (strong, nonatomic) id<ViewController2Delegate> delegate;
#protocol BexAPIClientDelegate <NSObject>
- (void)ViewController2:(ViewController2 *)vc didCalculateCost:(CGFloat)cost;
#end
Then at the end of the getIt() call the delegate
[self.delegate viewController2:self didCalculateCost:gasCostPerMile];
You need to set ViewController3 to be ViewController2's delegate when you load it. Also you will need to setup ViewController3 to conform to the protocol.
Using the protocol is worth the effort as it will make your code clear and easy to manage.
Retain a string in VC3. While initializing the VC3 object, set the value from VC2. Update the label in VC3(in viewDidLoad method)

"property 'textField' not found on object of type 'MainUI *'"

I have multiple text fields on one view controller. My .m and .h are below. I'm new to Xcode and Objective-C overall so please kind if this is just a stupid mistake.
.h:
#import <UIKit/UIKit.h>
#interface MainUI : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *letters;
#property (weak, nonatomic) IBOutlet UISegmentedControl *par;
#property (weak, nonatomic) IBOutlet UISegmentedControl *basicSym;
#property (weak, nonatomic) IBOutlet UITextField *numLetters;
#property (weak, nonatomic) IBOutlet UIButton *mainBack;
#property (weak, nonatomic) IBOutlet UIButton *mainNext;
#end
.m:
#import "MainUI.h"
#interface MainUI ()
#end
#implementation MainUI
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)textFieldShouldReturn: (UITextField*)theTextField {
if (theTextField == self.textField) {
[theTextField resignFirstResponder];
}
return YES;
}
#end
The error is at the if (theTextField == self.textField) { help please?
If you're just trying to make the keyboard go away when the user presses Return while the cursor is in a textfield, this should be sufficient:
- (BOOL)textFieldShouldReturn: (UITextField*)theTextField {
[theTextField resignFirstResponder];
return YES;
}

Resources