I used the following code to Webview navigation.
The following code works well some URLS, but it doesnt work some URL.. I dont know what i did mistake in my code...
.h
#import <UIKit/UIKit.h>
#interface WebviewViewController : UIViewController <UIWebViewDelegate>
{
IBOutlet UIWebView * webview;
IBOutlet UIToolbar * tBar;
IBOutlet UIBarButtonItem * backbtn;
IBOutlet UIBarButtonItem * forwardbtn;
}
#property (nonatomic, retain) IBOutlet UIWebView * webview;
#property (nonatomic, retain) IBOutlet UIBarButtonItem * backbtn;
#property (nonatomic, retain) IBOutlet UIBarButtonItem * forwardbtn;
#property (nonatomic, retain) UIToolbar * tBar;
- (IBAction)goBackbttn:(id)sender;
- (IBAction)goForwardbttn:(id)sender;
#end
.m
#import "WebviewViewController.h"
#implementation WebviewViewController
#synthesize webview;
#synthesize tBar, backbtn, forwardbtn;
- (void)viewDidLoad {
[super viewDidLoad];
[backbtn setEnabled : FALSE];
[forwardbtn setEnabled : FALSE];
NSString * urlAddress = # "http://www.exampl.com";
NSURL * url = [NSURL URLWithString:urlAddress];
NSURLRequest * requestObj = [NSURLRequest requestWithURL:url];
webview.scalesPageToFit = YES;
webview.autoresizesSubviews = YES;
[webview loadRequest : requestObj];
}
- (IBAction)goBackbttn:(id)sender
{
[forwardbtn setEnabled : TRUE];
}
- (IBAction)goForwardbttn:(id)sender {
[webview goForward];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[backbtn setEnabled :[webView canGoBack]];
[forwardbtn setEnabled :[webView canGoForward]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[super dealloc];
}
#end
Shouldn't this
- (IBAction)goBackbttn:(id)sender
{
[forwardbtn setEnabled : TRUE];
}
be this:
- (IBAction)goBackbttn:(id)sender
{
[webview goBack];
}
?
Related
I am parsing JSON using AFNetworking.
i passed 2 images and text from one view to another but i cant view the image on the UIImage view.
I can see the link of the images on the console.
here is my code (link to github)
#import "KKDetailsViewController.h"
#interface KKDetailsViewController ()
#end
#implementation KKDetailsViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.textView.text = [self.importVideoMetaData objectForKey:#"description"];
self.imageView.image = [[UIImage alloc] init];
self.imageView.image = [UIImage imageNamed:[(NSDictionary *)self.importThumbnail objectForKey:#"hqDefault"]];
NSLog(#" imported thumbnail %#", self.importThumbnail);
}
here is the .h file
#import <UIKit/UIKit.h>
#interface KKDetailsViewController : UIViewController
#property (weak, nonatomic) NSDictionary *importVideoMetaData;
#property (weak, nonatomic) NSDictionary *importThumbnail;
#property (weak, nonatomic) IBOutlet UITextView *textView;
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
#end
ok here is how i made it work:
- (void)viewDidLoad
{
[super viewDidLoad];
self.textView.text = [self.importVideoMetaData objectForKey:#"description"];
NSURL *url = [[NSURL alloc] initWithString:[self.importThumbnail objectForKey:#"hqDefault"]];
[self.imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:#"fakeThumbnail.png"]];
NSLog(#" imported thumbnail %#", url);
}
so like im sure every iOS programmer i ran into a problem, when trying to have a page full of UITextFields, it became evident that when the keyboard comes up it covers half of them. So i went to Apple's documentation and read all about it and wrote up the code for it, but it isn't working for me. Every time i run the view it crashes. Thanks in advance for your help.
.h file
#interface NewUserViewController : UIViewController{
}
- (IBAction)signMeUpButtonPressed:(id)sender;
#property (weak, nonatomic) IBOutlet UITextField *activeField;
#property (weak, nonatomic) IBOutlet UIScrollView *contentScrollView;
#property (weak, nonatomic) IBOutlet UIButton *backButton;
#property (weak, nonatomic) IBOutlet UILabel *firstNameLabel;
#property (weak, nonatomic) IBOutlet UITextField *firstNameInput;
#property (weak, nonatomic) IBOutlet UILabel *lastNameLabel;
#property (weak, nonatomic) IBOutlet UITextField *lastNameInput;
#property (weak, nonatomic) IBOutlet UILabel *usernameLabel;
#property (weak, nonatomic) IBOutlet UITextField *usernameInput;
#property (weak, nonatomic) IBOutlet UILabel *emailLabel;
#property (weak, nonatomic) IBOutlet UITextField *emailInput;
#property (weak, nonatomic) IBOutlet UILabel *passwordLabel;
#property (weak, nonatomic) IBOutlet UITextField *setPasswordInput;
#property (weak, nonatomic) IBOutlet UILabel *reenterPasswordLabel;
#property (weak, nonatomic) IBOutlet UITextField *checkSetPasswordInput;
#property (weak, nonatomic) IBOutlet UIButton *signMeUpButton;
All are connected to their outlets (except for active field. Im not sure where it belongs or what it should be connected to)
.m file
#implementation NewUserViewController
#synthesize contentScrollView;
#synthesize activeField;
#synthesize firstNameLabel, lastNameLabel, usernameLabel, emailLabel, passwordLabel, reenterPasswordLabel;
#synthesize firstNameInput, lastNameInput, usernameInput, emailInput, setPasswordInput, checkSetPasswordInput;
#synthesize backButton, signMeUpButton;
- (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.
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
contentScrollView.contentInset = contentInsets;
contentScrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
[contentScrollView setContentOffset:scrollPoint animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
contentScrollView.contentInset = contentInsets;
contentScrollView.scrollIndicatorInsets = contentInsets;
}
- (IBAction)signMeUpButtonPressed:(id)sender {
}
Try this:
- (void)keyboardWasShown:(NSNotification *)notification {
NSDictionary* userInfo = [notification userInfo];
CGRect keyboardEndFrame;
[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
//offset is global
offset = contentScrollView.contentOffset;
CGRect viewFrame = contentScrollView.frame;
CGRect keyboardFrame = [self convertRect:keyboardEndFrame toView:nil];
viewFrame.size.height -= keyboardFrame.size.height;
contentScrollView.frame = viewFrame;
UITextField *current = (UITextField *)[self findFirstResponder];
CGRect textFieldRect = current.frame;
[contentScrollView scrollRectToVisible:textFieldRect animated:YES];
}
- (UIView *)findFirstResponder {
for (UIView *subView in scrollView.subviews) {
if ([subView isFirstResponder]){
return subView;
}
}
return nil;
}
- (void) keyboardWillBeHidden:(NSNotification *)notification {
//scrollViewFrame is global the first frame of you scrollview
contentScrollView.frame = scrollViewFrame;
contentScrollView.contentOffset =offset;
}
this is not a complete code this is just a little logic of performing what you want to do.
I am doing some exercises with delegate, but now I have a problem with a UIView. This is my storyboard
I want to change the color of the UIView with 3 UISliders. The range of UISliders is from 0 to 255.
And this is my code:
ColorField is the UIView custom class
ColorField.h
#import <UIKit/UIKit.h>
#protocol ColorFieldDelegate <NSObject>
-(NSArray *)giveMeColors;
#end
#interface ColorField : UIView
#property (nonatomic , weak) IBOutlet id<ColorFieldDelegate> delegate;
#end
ColorField.m
#import "ColorField.h"
#implementation ColorField
#synthesize delegate = _delegate;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
NSArray *arrayOfColors = [self.delegate giveMeColors];
int red = [[arrayOfColors objectAtIndex:0] intValue];
int green = [[arrayOfColors objectAtIndex:1] intValue];
int blue = [[arrayOfColors objectAtIndex:2] intValue];
NSLog(#"Red --> %d" ,red);
NSLog(#"Green --> %d" ,green);
NSLog(#"Blue --> %d \n\n" ,blue);
self.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}
#end
ColorViewController.h
#import <UIKit/UIKit.h>
#import "ColorField.h"
#interface ColorViewController : UIViewController <ColorFieldDelegate>
#property (nonatomic) IBOutlet ColorField *colorField;
#property (weak, nonatomic) IBOutlet UISlider *redSlider;
#property (weak, nonatomic) IBOutlet UISlider *greenSlider;
#property (weak, nonatomic) IBOutlet UISlider *blueSlider;
#end
ColorViewController.m
#import "ColorViewController.h"
#interface ColorViewController ()
#property (nonatomic) double redQuantity;
#property (nonatomic) double greenQuantity;
#property (nonatomic) double blueQuantity;
#end
#implementation ColorViewController
#synthesize colorField = _colorField;
#synthesize redSlider;
#synthesize greenSlider;
#synthesize blueSlider;
#synthesize redQuantity;
#synthesize blueQuantity;
#synthesize greenQuantity;
- (void)viewDidLoad
{
[super viewDidLoad];
[self.colorField setDelegate:self];
[self.colorField setNeedsDisplay];
self.redQuantity = 125.0;
self.blueQuantity = 125.0;
self.greenQuantity = 125.0;
[self.colorField setNeedsDisplay];
// Do any additional setup after loading the view, typically from a nib.
}
-(ColorField *)colorField
{
if (_colorField == nil) {
_colorField = [[ColorField alloc] init];
}
return _colorField;
}
- (void)viewDidUnload
{
[self setColorField:nil];
[self setRedSlider:nil];
[self setGreenSlider:nil];
[self setBlueSlider:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
-(IBAction)changeRedQuantity:(UISlider *)sender
{
//methods of UISliders
self.redQuantity = [sender value];
[self.colorField setNeedsDisplay];
}
-(IBAction)changeBlueQuantity:(UISlider *)sender
{
self.blueQuantity = [sender value];
[self.colorField setNeedsDisplay];
}
-(IBAction)changeGreenQuantity:(UISlider *)sender
{
self.greenQuantity = [sender value];
[self.colorField setNeedsDisplay];
}
-(NSArray *)giveMeColors
{
NSNumber *redNumber = [NSNumber numberWithDouble:self.redQuantity];
NSNumber *greenNumber = [NSNumber numberWithDouble:self.greenQuantity];
NSNumber *blueNumber = [NSNumber numberWithDouble:self.blueQuantity];
NSArray *array = [[NSArray alloc] initWithObjects:redNumber, greenNumber,blueNumber, nil];
return array;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
BUT...this is the result: It show me only RED, GREEN and BLUE colors without gradation, for example: RGB (255,0,75) is THIS
and not THIS:
I don't know with it can't show me gradation...
Thanks!!
Marco Manzoni
You have to divide your slidervalues by 255.0.
[UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0];
colorWithRed only accepts values 0.0-1.0
I have a problem with my app (ipad) on iOS4. The Keyboard doesn't work, doesn't display on my webview AFTER the first view (login view).
Appdelagate :
AppDelegate.h
#class RootViewController;
#interface StandardFacileAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
RootViewController *rootViewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet RootViewController *rootViewController;
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
rootViewController = [[RootViewController alloc] init];
window.rootViewController = rootViewController;
//[window addSubview:rootViewController.view];
[window makeKeyAndVisible];
return YES;
}
RootViewController :
- (void)viewDidLoad {
[super viewDidLoad];
[self loadViewController];
[self.view addSubview:viewController.view];
}
- (void)loadViewController {
ViewController *viewControllertemp = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
self.viewController = viewControllertemp;
[viewControllertemp release];}
viewController is my login view after request i call the method for change the view (my webview) :
- (void)changeViewToIpadTheWebView:(UIViewController *)fromView{
UIViewController *currentView = (UIViewController*)fromView;
NSLog(#"currentView =>%#",currentView);
if(!ipadTheWebView)
[self loadipadTheWebView];
[currentView.view removeFromSuperview];
for (UIView *view in [self.view subviews]) {
[view removeFromSuperview];
}
[self.view addSubview:ipadTheWebView.view];
NSURL *url = [NSURL URLWithString:#"http://www.google.com/"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[ipadTheWebView.webView loadRequest:requestURL];
My webview is ok but if i want search word in google, the keyboard doesn't display (the view scroll) when the textfield have the focus.
What is the problem, this code working on iOS5 but not ios4?
Thanks for your help ;)
Edit :
Thanks Mathieu,
RootViewController.h
#import <UIKit/UIKit.h>
#class ViewController;
#class IpadTheWebView;
#interface RootViewController : UIViewController {
ViewController *viewController;
IpadTheWebView *ipadTheWebView;
}
#property (nonatomic,retain) ViewController *viewController;
- (void)loadViewController;
- (void)changeViewToViewController:(UIViewController *)fromView;
#property (nonatomic,retain) IpadTheWebView *ipadTheWebView;
- (void)loadipadTheWebView;
- (void)changeViewToIpadTheWebView:(UIViewController *)fromView;
- (void)keyboardWillShow:(NSNotification *)notification;
- (void)keyboardWillHide:(NSNotification *)note;
#end
(I'm beginner.)
I'm practicing Navigation Controller. I try to implement a simple calculator.
I ran the code in simulator. After I pressed any button which was linked to "addFunction", "substractFunction", "multiplyFunction" or "divideFunction", It crashed.
The debugger marked the following code in main.m
int retVal = UIApplicationMain(argc, argv, nil, nil);
and said "Thread 1: Program received signal: "SIGABRT"."
Does anyone know how to cope with this situation? Thanks.
Here's the code:
ChangeAppView.h:
#import <UIKit/UIKit.h>
#class ChangeViewController;
#interface ChangeAppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
UINavigationController *navigationController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) UINavigationController *navigationController;
#end
ChangeAppDelegate.m:
#import "ChangeAppDelegate.h"
#import "ChangeViewController.h"
#implementation ChangeAppDelegate
#synthesize window;
#synthesize navigationController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
navigationController = [[UINavigationController alloc] init];
self.window.rootViewController = navigationController;
ChangeViewController *changeViewController = [[ChangeViewController alloc] initWithNibName:#"ChangeViewController" bundle:nil];
[navigationController pushViewController:changeViewController animated:YES];
[changeViewController release];
[self.window makeKeyAndVisible];
return YES;
}
…
- (void)dealloc
{
[navigationController release];
[window release];
[super dealloc];
}
#end
CalculatorViewController.h:
#import <UIKit/UIKit.h>
#interface CalculatorViewController : UIViewController
{
IBOutlet UITextField *numberField1;
IBOutlet UITextField *numberField2;
IBOutlet UILabel *resultLabel;
}
#property (nonatomic , retain) IBOutlet UITextField *numberField1;
#property (nonatomic , retain) IBOutlet UITextField *numberField2;
#property (nonatomic , retain) IBOutlet UILabel *resultLabel;
-(IBAction)addFunction:(id)sender;
-(IBAction)substractFunction:(id)sender;
-(IBAction)multiplyFunction:(id)sender;
-(IBAction)divideFunction:(id)sender;
-(IBAction)clear:(id)sender;
-(IBAction)backgroundTap:(id)sender;
#end
CalculatorViewController.m:
#import "CalculatorViewController.h"
#implementation CalculatorViewController
#synthesize numberField1;
#synthesize numberField2;
#synthesize resultLabel;
-(IBAction)addFunction:(id)sender
{
float a = ([numberField1.text floatValue]);
float b = ([numberField2.text floatValue]);
resultLabel.text = [NSString stringWithFormat:#"%2.f" , a+b];
}
-(IBAction)substractFunction:(id)sender
{
float a = ([numberField1.text floatValue]);
float b = ([numberField2.text floatValue]);
NSString *result = [[NSString alloc] initWithFormat:#"%2.f" , a-b];
resultLabel.text = result;
[result release];
}
-(IBAction)multiplyFunction:(id)sender
{
float a = ([numberField1.text floatValue]);
float b = ([numberField2.text floatValue]);
resultLabel.text = [[NSString alloc] initWithFormat:#"%2.f" , a*b];
}
-(IBAction)divideFunction:(id)sender
{
float a = ([numberField1.text floatValue]);
float b = ([numberField2.text floatValue]);
resultLabel.text = [[NSString alloc] initWithFormat:#"%2.3f" , a/b];
}
-(IBAction)clear:(id)sender
{
numberField1.text = #"";
numberField2.text = #"";
resultLabel.text = #"";
}
-(IBAction)backgroundTap:(id)sender
{
[numberField1 resignFirstResponder];
[numberField2 resignFirstResponder];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[numberField1 release];
[numberField2 release];
[resultLabel release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
self.title = #"Calculator";
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
From the exception above, it seems that your IBActions are not properly connected.
As dasdom mentioned, delete all your buttons, create new buttons and then add IBAction methods accordingly.
Also one more thing i recognized in your code, in the multiply and divide methods there is a memory leak.You have written
resultLabel.text = [[NSString alloc] initWithFormat:#"%2.f" , a*b];
it should be
resultLabel.text = [[[NSString alloc] initWithFormat:#"%2.f" , a*b]autorelease];
or
resultLabel.text = [NSString StringWithFormat:#"%2.f" , a*b];
and do a similar change in divide method also.
To what did you link your backgroundtap method?
It seems that you buttons aren't buttons. They seem to be view. Delete the buttons from you nib and put new buttons there and link them you our IBActions.