Use of undeclared identifier in Objective-C, trying to set up ScrollView - ios

I'm trying to set up a scrollView for my content in my app, but even though I declare the scroller in the .h code, when I try to use it in the the .m file I get a 'use of undeclared identifier' error.
This is the code in my .h file:
#interface ViewController :UIViewController <MFMailComposeViewControllerDelegate> {
IBOutlet UIScrollView *scroller;
}
And this is the code in my .m file:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[scroller setScrollEnabled:YES]; //error here
[scroller setContentSize:CGSizeMake(320, 700)]; //and error here
}
Any help would be greatly appreciated.
Thanks

Related

Display nib above of my storyboard view

I'm trying to essentially load a nib into view when a certain criteria is met. I'm able to show the nib on my storyboard thought I cant seem to be able to execute any actions when the button is pressed.
Just to express my ultimate goal. I would like to create sort of a template with my nib view and then display the nib view from my storyboards but manipulate the labels in my nib view with values sent from the storyboard.
OK so I'll do my best to try and show my detail steps on how i try to accomplish this.
Firstly here are is a picture of my project.
my VCDemoView.h
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#end
VCDemoView.m
#import "VCDemoView.h"
#interface VCDemoView ()
#property(nonatomic) IBOutlet UIImageView *imageView;
#property(nonatomic) IBOutlet UILabel *titleLabel;
#property(nonatomic) IBOutlet UILabel *subtitleLabel;
#property(nonatomic) IBOutlet UIView *container;
//- (IBAction)changeLabel:(id)sender;
#end
#implementation VCDemoView
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self == nil) return nil;
[self initalizeSubviews];
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self == nil) return nil;
[self initalizeSubviews];
return self;
}
-(void)initalizeSubviews
{
//Load the contents of the nib
NSString *nibName = NSStringFromClass([self class]);
UINib *nib = [UINib nibWithNibName:nibName bundle:nil];
[nib instantiateWithOwner:self options:nil];
//Add the view loaded from the nib into self.
[self addSubview:self.container];
}
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
- (IBAction)changeLabel:(id)sender {
//[self.subtitleLabel.text = #"MIGUEL"];
_subtitleLabel.text = #"miguel";
}
#end
storyboard view controller.
#import "ViewController.h"
#import "VCDemoView.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
VCDemoView * CustomView = [[VCDemoView alloc]init] ;//[[addMyLocation alloc]init];
[self.view addSubview:CustomView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Can anyone spot what I'm doing wrong? I cant seem to be able to communicate with the button? I'm sure is something silly.
here is a link to the dropbox project with all the source code if you prefer to see it that way.
https://www.dropbox.com/sh/23cbhs4qvsxwei0/AADFs6MN3eqJNK42Io2etuJea?dl=0
Thanks is advance guys.
You never set the frame of your VCDemoView. The frame defaults to CGRectZero.
A view doesn't clip its subviews by default, so you can still see the labels and the button, even though they're outside the bounds of the VCDemoView. But a view does not receive events outside its bounds, so when you tap on the button, the top-level view swallows the event because it finds that the event is outside the bounds of its one subview (the VCDemoView).
You can fix the frame of your VCDemoView like this:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
VCDemoView * CustomView = [[VCDemoView alloc]init] ;//[[addMyLocation alloc]init];
CustomView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
CustomView.frame = self.view.bounds;
Note that you can use autoresizing even though your storyboard uses autolayout constraints. Autolayout will turn the autoresizing mask into constraints for you.
Or you could just put your VCDemoView in your storyboard as a child of the root view and set up the constraints there.
You should not call and action inside a View this what the Controller should handle .
You need to change the fileOwner to be the controller and the View Class to be VCDemoView , connect the View to the controller in the IB , and load the nib from there , then add it as a subView to the ViewController's view .
this the edited code ,, I hope it helps .. https://dl.dropboxusercontent.com/u/33359624/demo-SO/Archive.zip
You are not set the CustomView frame so the default frame is CGRectZero. if you set the frame of the CustomView it will works fine.
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
VCDemoView * CustomView = [[VCDemoView alloc]init] ;
CustomView.frame =self.view.frame;
[self.view addSubview:CustomView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

Image Not Showing Up on UIScrollView

So I have a UIScrollView and scrolling works. I added a label on it and it scrolled that too. When I added an Image View, however, scrolling works but the image does not show up and I get this error:
Could not load the "block" image referenced from a nib in the bundle with identifier
My ViewController.m:
#interface ViewController : UIViewController{
IBOutlet UIScrollView * scroller;
}
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320, 600)];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#end
I have disabled "Use Auto Layout," Changed the Size to "Freeform," and changed the views height to 600.
Any ideas on how I can make this work?

UIScrollView doesn’t seem to work in xcode 6 (iOS 8)

I’m trying to make a scroll view in xcode. I used the following code in xcode 5 (which worked):
view.h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
IBOutlet UIScrollView *ScrollView;
}
#end
view.m
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[ScrollView setScrollEnabled:YES];
[ScrollView setContentSize:CGSizeMake(320, 1005)];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I have seen others with this problem but none of their solutions worked for me. Note that i have size set to freedom and auto layout is turned off.
Try this - UIScrollView with Autolayout
OR
try setting your content size in viewDidAppear
Due to a scrollView feature in auto layout, the contentSize is being redefined. To solve this problem, you could disable auto layout or set the content size in viewDidLayoutSubviews()
-(void)viewWillLayoutSubviews {
[super viewDidLayoutSubviews];
ScrollView.contentSize = CGSizeMake(320,1005);
}
So you would want to add this code and delete the ScrollView.contentSize method in viewDidLoad.
I don't see anything immediately wrong with this code. I would check and makes sure that you've connected the IBOutlet properly in the Interface Builder.

ios UiPickerView with CountryPicker class

Im trying to add the UiPickerView class from https://github.com/nicklockwood/CountryPicker
I want to use with storyboard in my viewController but the delegate method is never getting called although the pickerView shows the countries.
Here is what ive done:
ive imported the CountryPicker folder and Flags folder, in the storyboard controller ive added a UiPickerView and assign it to CountryPicker class.
The ViewController.h and .m files are exactly the same as the example.
i cant find what im missing.
Also i tried init the object and call the setSelectedCountry method from my ViewController.m file but its also not doing anything.
ViewController.h
#import <UIKit/UIKit.h>
#import "CountryPicker.h"
#interface ViewController : UIViewController <CountryPickerDelegate>
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
CountryPicker *picker = [[CountryPicker alloc] init];
[picker.delegate self];
[picker setSelectedCountryCode:#"US" animated:YES];
}
- (void)countryPicker:(__unused CountryPicker *)picker didSelectCountryWithName:(NSString *)name code:(NSString *)code
{
NSLog(#"country: %#",name);
}
#end
You need to create an IBOutlet from the storyboard to your code Apple Docs
Remove the code you have in your viewDidLoad that creates a new picker.
Then for the the new IBOutlet you've created, set self as the delegate, so if your outlet was called picker your viewDidLoad should look as follows:
- (void)viewDidLoad
{
[super viewDidLoad];
self.picker.delegate = self;
[self.picker setSelectedCountryCode:#"US" animated:YES];
}

Can not dismiss keyboard iOS. Nothing works

I'm new in iOS Development for iPhone. I can not dismiss keyboard, nothing works. Please help me, maybe I didn't notice anything?
My .h file:
#import "UIKit/UIKit.h"
#interface ViewController : UIViewController <UITextViewDelegate>
#property (weak, nonatomic) IBOutlet UITextField *textField;
#end
My .m file:
#import "ViewController.h"
#import "UIKit/UIKit.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.
}
-(BOOL) textFieldShouldReturn: (UITextField *) textField{
[textField resignFirstResponder];
return YES;
}
#end
Hi this is a simple mistake. You have not set the delegate. You can do this by..
- (void)viewDidLoad
{
[super viewDidLoad];
textField.delegate = self;
}
Please add textField.delegate = self; to your viewDidLoad.
Most likely you forgot to set the delegate for textField. Inside your storyboard or nib file, drag your UITextField's delegate to your ViewController.

Resources