Pushing UIWebView onto UIViewController - uiwebview

Almost all the examples I see are done with IB but I don't want to use IB.
What I want to do is, when a user selects a row in the table a UIWebView will be pushed onto the stack and load that particular page while keeping the tab bar and navigation bar. I don't want all the features of a browser rather just be able to scroll through a page beacause the rest of my app controls how a person can navigate through the website through tables.
So I've been able to push other viewcontrollers but pushing a UIWebView with the same method doesn't work.
Here is what I have so far..
This is the Threads.h file
#import "ThreadContent.h"
#import <UIKit/UIKit.h>
#interface Threads : UITableViewController {
NSMutableArray *threadName;
NSMutableArray *threadTitle;
UIActivityIndicatorView *spinner;
NSOperationQueue *operationQueue;
UILabel *loadingLabel;
NSMutableDictionary *cachedForum;
NSMutableArray *forumID;
NSInteger *indexPathRowNumber;
NSMutableArray *threadID;
ThreadContent *threadContent;
}
#property (nonatomic, assign) NSMutableArray *forumID;
#property (nonatomic, assign) NSInteger *indexPathRowNumber;
#end
Part of my Threads.m file where I am trying to push the UIWebView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"%i",indexPath.row);//get row number
NSLog(#"%#", [threadID objectAtIndex:indexPath.row]);//thread id
//forums.whirlpool.net.au/forum-replies.cfm?t=
//NSString *urlString = [NSString stringWithFormat:#"forums.whirlpool.net.au/forum-replies.cfm?t=%#", [threadID objectAtIndex:indexPath.row]];
threadContent = [[ThreadContent alloc] init];
[self.navigationController pushViewController:threadContent animated:YES];
}
My WebView files.. well i'm not sure how to do that? I did make it a "UIWebView" subclass but if i try to push it on to the stack i get a crash saying it needs it to be a "UIViewController" subclass.

UIWebView is a subclass of UIView not UIViewController.
You need to subclass UIViewController (call it for example WebViewController)
And in the viewDidLoad method create a UIWebView and add it to the view, using addSubview:
You can pass the URL to load in the webView as a property of the WebViewController
-(void)viewDidLoad {
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubView:webView];
[webView loadRequest:[NSURLRequest requestWithURL:self.urlToLoad]];
[webView release];
}
and in Threads
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"%i",indexPath.row);//get row number
NSLog(#"%#", [threadID objectAtIndex:indexPath.row]);//thread id
//forums.whirlpool.net.au/forum-replies.cfm?t=
//NSString *urlString = [NSString stringWithFormat:#"forums.whirlpool.net.au/forum-replies.cfm?t=%#", [threadID objectAtIndex:indexPath.row]];
threadContent = [[ThreadContent alloc] init];
threadContent.urlToLoad = [NSURL URLWithString:urlString];
[self.navigationController pushViewController:threadContent animated:YES];
}
(or make the webView a property and call the load method in just before or after you push the WebViewController in Threads, I'm not 100% sure if this way would work)

Related

CoreData UINavigationController managedObjectContext Error

I have a project that include a combined tabbarController and navigation controller.
sorry,becauce i can't post image, the link of image here:
http://s12.postimage.org/58lbzzxm5/Screen_Shot_2012_11_10_at_9_56_07_AM.png
Code Of NAppDelegate.h:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#interface NAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
#property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
#end
Code Of NAppDelegate.m:
#import "NAppDelegate.h"
#import "TVC_TabProvince.h"
#implementation NAppDelegate
#synthesize managedObjectContext = _managedObjectContext;
#synthesize managedObjectModel = _managedObjectModel;
#synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
NSArray *arrController = [tabBarController viewControllers];
TVC_TabProvince *controller = (TVC_TabProvince*) [arrController objectAtIndex:0];
controller.managedObjectContext = self.managedObjectContext;
return YES;
}
Before adding the navigation controller, it run without any problem. But when I add this, an error appear:
error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-
[UINavigationController setManagedObjectContext:]: unrecognized selector sent to
instance 0x74b26b0'
The problem is in this line:
TVC_TabProvince *controller = (TVC_TabProvince*) [arrController objectAtIndex:0];
[arrController objectAtIndex:0] is the first view controller of the tab bar controller, so this is the navigation controller and not the TVC_TabProvince controller. The type cast (TVC_TabProvince *) does not change the object, it is still a navigation controller.
So you have to add one step:
NSArray *arrController = [tabBarController viewControllers];
UINavigationController *navController = [arrController objectAtIndex:0];
TVC_TabProvince *controller = [navController. viewControllers objectAtIndex:0];
controller.managedObjectContext = self.managedObjectContext;
It might be more flexible to go the other way around: Instead of "pushing" the managed object context from the application delegate to the table view controller, you "pull" it from the table view controller when needed. So somewhere in "TVC_TabProvince.m" you do
NAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
self.context = [appDelegate managedObjectContext];
Then it does not matter anymore where the table view controller is in the view controller hierarchy. (But that is only a suggestion.)

UIViewController viewDidLoad not call

I am new in ios development. I want to develop custom navigation button method.
-(void) handleNext:(id)sender
{
MRGAppDelegate *appDelegate = (MRGAppDelegate*)
[[UIApplication sharedApplication]delegate];
[appDelegate.viewController GotoDirectoryView:self.restListViewController calledView:self.view];
}
in MRGViewController.m
-(void) GoToDirectoryView:(RestaurantListViewController*) resViewContrller calledView:(UIView*)viewControllerView
{
self.resListViewController = resViewController;
[resViewController release];
[viewControllerView removeFromSuperview];
[self.resListViewController viewDidLoad];
[self.view addSubview:self.resListViewController.view];
}
But Don't call to RestaurantListViewController viewDidLoad don't call. No error appear. I don't know why? Please help me.
viewDidLoad, viewWillAppear etc are view controller life cycle method, these are called automatically when view is loaded on navigation stack. You won't need to call these method at all.
Currently you are simply adding a view on existing view controller, while you need to load a new view controller if you want view controller life cycle methods to get called.
Use like -
[self.navigationController pushViewController:restListViewController animated:YES];
[restListViewController release];
EDIT 1 -
restListViewController = [[RestaurantListViewController alloc] init];
[self presentModalViewController:restListViewController animated:YES];
[restListViewController release];

How to add initial UIViewController to existing COreData app?

I am using model "Master-detail application" for my app and would like to set different initial view. Is it possible? I want to display UIViewController with menu buttons and one of these buttons should load the Navigation controller of the previous original initial view.
When I change the initial view in storyBoards UI to ViewController and link the button to NavigationController , the app crushes.
What would be the right thing to do?
Thank you for your time!
Include this code to the AppDelegate:
- (void)tellDelegateToFlip{
MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:#"MasterViewController" bundle:nil];
masterViewController.managedObjectContext = self.managedObjectContext;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4];
[UIView setAnimationTransition:UIViewAnimationOptionTransitionFlipFromLeft forView:self.window cache:YES];
[self.navigationController pushViewController:masterViewController animated:YES];
[UIView commitAnimations];
}
And this method to the Initial view to call the master view:
-(IBAction)flipToMaster:(id)sender{
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate tellDelegateToFlip];
}
And You're done!

addSubview in UIViewController's View has no response

Newbie question. I created a UIView named TestView with 2 IBOutlet UIButton. I would like to add this view to initially launched view controller.
The header file of the TestView is as follow:
#import <UIKit/UIKit.h>
#interface TestView : UIView {
IBOutlet UIButton *btn1;
IBOutlet UIButton *btn2;
}
#end
I try to add the view to the screen by:
TestView *view = [[TestView alloc] init];
[self.view addSubview:view];
[view release];
but I got no response. How can I add TestView to the original view ?
p.s. TestView as a XIB too, named TestView.xib
Environment:
xCode 4
iOS 4.3.2
u have to specify the frame
frame means orgin + size
here orgin (0,0)
size (200,200)
otherwise iOs dont know where to draw this view
TestView *view = [[TestView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[view setBackgroundColor:[UIColor greenColor]];
[view addSubview:btn1];
[view addSubview:btn2];
//here add whatever u want
[self.view addSubview:view];
[view release];
You shouldn't call it view because your UIView's got already an instance variable called view.
You could also create this using Interface Builder and linking your elements to your Outlets, it will be easier

NavigationController as 2nd View

On my main view controller I have a button that shows a new view.
- (IBAction)showInformation:(id)sender {
InfoViewController *controller = [[InfoViewController alloc] initWithNibName:#"InfoView" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[self.view addSubview:[controller view]];
}
On my InfoView.xib I added a UINavigationController and made the proper connections. However when I the InfoViewController loads is not showing the UINavigation as I want it to.
Currently I have this on viewDidLoad
self.view = [navController view];
And on the header file I have
IBOutlet UINavigationController *navController;
#property (nonatomic, retain) IBOutlet UINavigationController *navController;
What am I missing?
UINavigation did not show you mean UINavigationController did not appear?
1 If you use UINavigationController, you need at least two views: one for the default view that appears with the controller and another that is pushed two.
I can only see one view here, which is InfoViewController.
If you use UINavigationController, you may want to use the pushViewController method.
2 If you use presentModalViewController method, no UINavigationCOntroller is needed.

Resources