Why does my UIWebView not Allow User Interaction? - objective-c

I'm new to these forums so I apologize for my noobieness. I did as thorough a search as I could, but I couldn't find anyone else with this issue, applogise if this has been covered elsewhere.
I've created a very simple example of my problem. I'm sure I'm missing something but I can't for the life of me figure out what.
I'm creating a UIWebView and adding it to a custom view controller that inherits from UIViewController. When the app loads in the iPad simulator, the uiwebview loads the desired page, but the UIWebView is entirely unresponsive. The webview does not pan or scroll and none of the in page links can be clicked. However, if you change the orientation of the webview suddleny everything works.
Thanks in advance for your help!!
AppDelegate header
#import <UIKit/UIKit.h>
#import "EditorViewController.h"
#interface FixEditorTestAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
EditorViewController *editorView;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) EditorViewController *editorView;
#end
AppDelegate Implementation
#import "FixEditorTestAppDelegate.h"
#import "EditorViewController.h"
#implementation FixEditorTestAppDelegate
#synthesize window;
#synthesize editorView;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(#"application is loading");
editorView = [[EditorViewController alloc] init];
[window addSubview:[editorView view]];
[window makeKeyAndVisible];
return YES;
}
- (void)dealloc {
[window release];
[editorView release];
[super dealloc];
}
#end
View Controller header
#import <UIKit/UIKit.h>
#interface EditorViewController : UIViewController <UIWebViewDelegate> {
UIWebView *webView;
}
#property (nonatomic, retain) UIWebView *webView;
#end
View Controller Implementation
#import "EditorViewController.h"
#implementation EditorViewController
#synthesize webView;
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
NSLog(#"loadView called");
UIView *curView = [[UIView alloc] init];
webView = [[UIWebView alloc] init];
webView.frame = CGRectMake(20, 40, 728, 964);
webView.delegate = self;
webView.backgroundColor = [UIColor redColor];
[curView addSubview: webView];
self.view = curView;
[curView release];
}
//Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"viewDidLoad called");
NSURL *url = [[NSURL alloc] initWithString:#"http://www.nytimes.com"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[webView loadRequest:request];
[url autorelease];
[request release];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
- (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.
}
- (void)viewDidUnload {
webView.delegate = nil;
[webView release];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end

You should really be loading this from a nib, but if you're going to create it programmatically, you probably want to add this to loadView:
webView.userInteractionEnabled = YES;
webView.scalesPageToFit = YES;

I'm going to go out on a limb here, as you aren't getting much response..
There is nothing wrong with this code (out on a limb! i haven't tested it). Neither is there anything wrong with not using a .nib (but really, you would have to have a reason, right?).
Using a nib doesn't do anything magic, just pretty much what you have done here.
Your problem must be elsewhere.. can you interact with a different kind of view? A button, say? Maybe it is something to do with the window setup?

The issue is with the extra UIView you have in there. It isn't needed, and I think the touch events aren't getting enabled. I tried out the code and it didn't work like you said. Then I changed the EditorViewController to this, and it works now (all I did was to remove the extra UIView) dragging and select links now works.
- (void)loadView {
NSLog(#"loadView called");
webView = [[UIWebView alloc] init];
webView.frame = CGRectMake(20, 40, 728, 964);
webView.delegate = self;
webView.backgroundColor = [UIColor redColor];
self.view = webView;
}

Related

Custom delegate iOS 6 using ARC not working

I am a begginer with Objective-C programming.
I searched here and in other websites to a way to solve my problem but i did not find.
I want to create an login view before the system load. Then, after login i want to dismiss it.
On my project i use ARC and not use Storyboards.
When i debug and look into the function "efetuarLogin" the value of property delegate is 0x000000. I think it is blank, is not?
According to the tutorials and other tips i found on the internet, this is the right way to code delegate. Can you check this for me?
Here is the code:
Login.h
#import <UIKit/UIKit.h>
#class Login;
#protocol LoginDelegate <NSObject>
#required
- (void)loginDidFinish: (Login *)login;
#end
#interface Login : UIViewController{
__weak id<LoginDelegate> delegate;
}
#property (nonatomic, weak) id <LoginDelegate> delegate;
- (IBAction)efetuarLogin:(id)sender;
#end
Login.M
#import "Login.h"
#implementation Login
#synthesize delegate;
- (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 from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)efetuarLogin:(id)sender {
[delegate loginDidFinish:self];
}
#end
MainController.h
#import <UIKit/UIKit.h>
#import "Login.h"
#interface MainController : UITabBarController <LoginDelegate>
#end
MainController.m
#import "MainController.h"
#import "Login.h"
#import "ModalViews.h"
#import "Alerts.h"
#import "ViewPadrao.h"
#import "TableView.h"
#implementation MainController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
ModalViews *modalViews = [[ModalViews alloc] initWithNibName:#"ModalViews" bundle:nil];
Alerts *alerts = [[Alerts alloc] initWithNibName:#"Alerts" bundle:nil];
ViewPadrao *viewPadrao = [[ViewPadrao alloc] initWithNibName:#"ViewPadrao" bundle:nil];
TableView *tableView = [[TableView alloc] initWithNibName:#"TableView" bundle:nil];
self.viewControllers = #[modalViews, alerts, viewPadrao, tableView];
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
Login *login = [[Login alloc] initWithNibName:#"Login" bundle:nil];
[login setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentViewController:login animated:NO completion:nil];
}
- (void)loginDidFinish: (Login *)login{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Sua mensagem aqui" message:#"Mensagem" delegate:self cancelButtonTitle:#"Fechar" otherButtonTitles:nil];
[alert show];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
And the method didFinishLaunchingOptions of AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
mainController = [[MainController alloc] init];
window.rootViewController = mainController;
[window makeKeyAndVisible];
return YES;
}
Sorry for the high quantity of code. I am thankful since already!!
I am using the ViewDidAppear method to show Login to users. But when i dismiss and the MainController appears, the method viewdidapper creates the Login again.
How i do to show login once? When i tried to do this on viewdidload it did not work. The login did not appear.
In your MainViewController.m when you create login object
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
Login *login = [[Login alloc] initWithNibName:#"Login" bundle:nil];
login.delegate = self; //add this line
[login setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentViewController:login animated:NO completion:nil];
}
EDIT : After seeing OP's edit in the question (which should have been a different thread)
Your view Controller hierarchy is wrong. The ideal flow should be
1) Show Login controller directly from App delegate.
2) After successfull login, show MainViewController from Login Controller.

How to put a tab bar only in certain place in the app?

I want to put a tab bar for one place in my app. From there i can load some other view controllers, but that's it. I don't want it for my whole application. All examples I read through are making the tab bar controller as the rootviewcontroller of the window.
So how do i do that?
Thank you!
his may help you, Consider your view controller is HomeView where from you are going to push a tab bar controller, The HomeView is loaded in view at below:
#import "AppDelegate.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
HomeView *homeview = [[HomeView alloc] initWithNibName:#"HomeView" bundle:nil];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:homeview];
self.window.rootViewController = navigation;
[self.window makeKeyAndVisible];
return YES;
}
#end
then the .h, .m and .xib files of HomeView are going to be as follows:
In HomeView.h
#import <UIKit/UIKit.h>
#interface HomeView : UIViewController
{
IBOutlet UITabBarController *tabBarController;
}
-(IBAction)loadTabBar:(id)sender;
#end
and the HomeView.m is:
#implementation HomeView
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(IBAction)loadTabBar:(id)sender
{
[self.navigationController pushViewController:tabBarController animated:YES];
}
- (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
{
[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
and the .xib file must be as follows:
The tabBarController IBOutlet must be connected to the UITabBarController that on the .xib file. And that UITabBarController with two view controllers named FirstViewController, SecondViewController. Moreover that the HomeView must be inside a UINavigationController.
If not clarified with this answer, I'll update with detailed explanation.
The above is a way of loading tab bar controller using XIB method.
You can do this by coding like below by changing something in IBAction(button action) , In HomeView.m file:
#import "HomeView.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#implementation HomeView
-(IBAction)loadTabBar:(id)sender
{
FirstViewController *firstView = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:firstView, secondView, nil];
[self.navigationController pushViewController:tabBarController animated:YES];
}
#end
You have to make tabBar in app delegate and then add when ever you want using this
-(void)addTabBarController
{
AppDelegate *appdelegte =(AppDelegate*)[[UIApplication sharedApplication]delegate];
//[[[appdelegte navigationController] view]removeFromSuperview];
[[appdelegte window]addSubview:[[appdelegte tabcontroller]view]];
[[appdelegte tabcontroller]setSelectedIndex:0];
}

initWithFrame is filling the screen instead of creating a frame of the size I want

I'm new to Cocoa development and suspect that this is a noob issue.
I'm creating a simple background canvas with a bespoke UIView. I was expecting that I would be able to create this canvas to be slightly indented down from the main view so that I could add a toolbar at a later date. However, when I use initWithFrame (and any other init for that matter), the canvas is always created to be the size of the full screen rather than slightly smaller than the full screen. Even if I completely override the values for CGRect it doesn't make a difference. I've set a touchesBegan event and set the background colour to green to be able to determine success but all I get are a completely green screen and a touch event that works everywhere.
Please help - this is driving me mad and I can't find the answer anywhere on the web.
The relevant code is as follows (there actually isn't much more code than this in the whole app so far):
AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.objMainViewController = [[MainViewController alloc] init];
self.window.rootViewController = self.objMainViewController;
[self.window makeKeyAndVisible];
return YES;
}
MainViewController.h
#import <UIKit/UIKit.h>
#import "viewCanvas.h"
#interface MainViewController : UIViewController
#property (strong, nonatomic) viewCanvas *objViewCanvas;
-(id)init;
#end
MainViewController.m
#import "MainViewController.h"
#import "viewCanvas.h"
#implementation MainViewController
#synthesize objViewCanvas;
-(id)init
{
if (self = [super init]) {
CGRect frame = CGRectMake(100, 100, 100, 100);
objViewCanvas = [[viewCanvas alloc] initWithFrame:frame];
}
return self;
}
- (void)loadView {
self.view = objViewCanvas;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
viewCanvas.h
#import <UIKit/UIKit.h>
#import "viewCanvas.h"
#interface viewCanvas : UIView {
}
//Init
- (id) initWithFrame:(CGRect)frame;
//Events
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
#end
viewCanvas.m
#import "viewCanvas.h"
#implementation viewCanvas
//Standard inits
- (id) initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
[self setBackgroundColor:[UIColor greenColor]];
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Touches Began Canvas" message:#"Canvas Touches Began" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles: nil];
[alert show];
}
#end
I think by default the view of a view controller fills the whole screen, so you need to add a subview to that view that's your smaller viewCanvas object (BTW, you should conform to the naming rules and name your classes with capital letters, ViewCanvas). Try this in your MainViewController .m file:
-(id)init{
if (self = [super init]) {
CGRect frame = CGRectMake(100, 100, 100, 100);
objViewCanvas = [[ViewCanvas alloc] initWithFrame:frame];
self.contentView = [[UIView alloc] initWithFrame:CGRectMake(1, 1, 1, 1)]; //this frame doesn't matter
[self.contentView setBackgroundColor:[UIColor whiteColor]];
}
return self;
}
- (void)loadView {
self.view = self.contentView;
[self.view addSubview:objViewCanvas];
}

How Do I Update UIWebView After viewDidLoad?

This is my first iOS app, so I am probably missing something very simple. Please be kind. I have been tearing my hair out and I could really use some help.
Overview Of App
Basically, this is a single page application that just loads a UIWebView. I have an external accessory (bluetooth barcode scanner) that I connect and basically what I want to do is when the the app receives a scan, I want to call a method in my ViewController and update the UIWebView accordingly.
What Is Working
I am able to connect the scanner, load the first view, which loads the initial webpage, scan a barcode and call the method in my controller.
My Problem
I can't seem to figure out how to update the UIWebView from the method in my controller. It logs the url string to my debugger area, but never actually updates the webview. I am pretty sure I have some delegation wrong or something with my webview instance. There must be some glue code here that I am missing.
My Code HelloWorldViewController.h
#import <UIKit/UIKit.h>
#import "KScan.h"
#interface HelloWorldViewController : UIViewController <UIWebViewDelegate> {
IBOutlet UIWebView *page;
IBOutlet UILabel *myLabel;
Boolean IsFirstTime;
KScan *kscan;
}
- (void)setFirstTime;
- (void)DisplayConnectionStatus;
- (void)DisplayMessage:(char *)Message;
- (void)newBarcodeScanned:(NSString *)barcode;
- (void)loadBarcodePage:(NSString *)barcode;
#property (nonatomic, retain) KScan *kscan;
#property (nonatomic, retain) UIWebView *page;
#property (nonatomic, retain) UILabel *myLabel;
#end
My Code HelloWorldViewController.m
#import "HelloWorldViewController.h"
#import "common.h"
#implementation HelloWorldViewController
#synthesize myLabel;
#synthesize page;
#synthesize kscan;
- (void)setFirstTime
{
IsFirstTime = true;
}
- (void)viewDidLoad
{
self.kscan = [[KScan alloc] init];
[super viewDidLoad];
page.scrollView.bounces = NO;
//page.delegate = self;
[page loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://192.168.0.187:3000"]]];
}
- (void) newBarcodeScanned:(NSString *)barcode
{
NSLog(#"%s[%#]",__FUNCTION__, barcode);
[self loadBarcodePage:barcode];
}
- (void)loadBarcodePage:(NSString *)barcode
{
NSLog(#"%s",__FUNCTION__);
NSString *url = [[NSString alloc] initWithFormat:#"http://www.google.com/%#", barcode];
NSLog(#"%#", url);
[page loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
}
- (void)viewDidUnload
{
[myLabel release];
myLabel = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
- (void)dealloc {
[page release];
[kscan release];
[myLabel release];
[super dealloc];
}
#end
Basically, I am just trying to load google.com into my page webview when scanning a barcode. My log statements are being logged with the correct URL, but this line of code doesn't work.
[page loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
I am not getting any errors and my xCode debugging skills are not the greatest.
Any help would be greatly appreciated!
It looks like your webview is never allocated, or added to your main view. You are probably talking to a nil instance.
Unless your web view comes from a XIB file (which I doubt since it is not declared as an IBOutlet in your heder file) try adding something like this to your viewDidLoad:
self.page = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.page];

MySecondViewController won't display correctly

I have programmatically created a second view controller (MySecondViewController) which was all done and hooked up without using the visual object editor.
My problem is that when I run the app, only the original view controller (HelloWorldViewController) comes up; the second view controller does not show up. When I click on the view for HelloWorldViewController, nothing comes up.
Can you tell me what's wrong?
windowBasedAppAppDelegate.h
#import <UIKit/UIKit.h>
//-- Add a forward reference to the HelloWorldViewController class --
#class HelloWorldViewController;
#interface windowBasedAppAppDelegate : NSObject <UIApplicationDelegate> {
//-- create an instance of the view controller--
HelloWorldViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
//--Expose the view controller as a property--
#property (nonatomic, retain) IBOutlet HelloWorldViewController *viewController;
#end
windowBasedAppAppDelegate.m
#import "windowBasedAppAppDelegate.h"
#import "HelloWorldViewController.h"
#import "MySecondViewController.h"
#implementation windowBasedAppAppDelegate
#synthesize window;
#synthesize viewController;
//-- a second view controller object--
MySecondViewController *mySecondViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
//-- add the new view to the current window--
//-- instantiate the second view controller --
mySecondViewController = [[MySecondViewController alloc]
initWithNibName:nil
bundle:nil];
// -- add the view from the second controller --
//[window addSubview:mySecondViewController.view];
[window addSubview:viewController.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)dealloc
{
[MySecondViewController release];
[viewController release];
[window release];
[super dealloc];
}
#end
MySecondViewController.h
#import <UIKit/UIKit.h>
#interface MySecondViewController : UIViewController {
//-- create two outlets, label and button --
UILabel *label;
UIButton *button;
}
//-- expose the outlets as properties --
#property (nonatomic, retain) UILabel *label;
#property (nonatomic, retain) UIButton *button;
// -- declaring the IBAction ---
- (IBAction) buttonClicked: (id)sender;
#end
MySecondViewController.h
#import "MySecondViewController.h"
#implementation MySecondViewController
#synthesize label, button;
- (IBAction) buttonClicked: (id)sender {
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle:#"Action Invoked!" message:#"Button clicked" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
HelloWorldViewController.h
#import <UIKit/UIKit.h>
#interface HelloWorldViewController : UIViewController {
}
#end
HelloWorldViewController.m
import "HelloWorldViewController.h"
#implementation HelloWorldViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
Added code from MySecondViewController.m
//-- add the views to the view window --
[self.view addSubview:label];
[self.view addSubview:button];
[view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
self.view = view;
[super viewDidLoad];
Since the view is being initialized and that it has a proper frame, the problem could be this sequence of code,
[window addSubview:mySecondViewController.view];
[window addSubview:viewController.view];
As the viewController's view will have the same frame, it will be added over mySecondViewController's view. So you will have to remove the second line for the view to show up.
You never actually push the new view controller onto the screen.
mySecondViewController = [[MySecondViewController alloc]
initWithNibName:nil
bundle:nil];
// -- add the view from the second controller --
//[window addSubview:mySecondViewController.view];
[window addSubview:viewController.view];
You add viewController.view, (which should be undefined...), but not mySecondViewController.view.
Also, programHasFinishedLaunching may not be the best possible place to push it. If I were you, I'd put a button on the front page that pushes the new view in, it's easier that way.