I am developing an app for the iphone. One of my views contains a textbox that I used to display text. I used storyboard to do this. To better explain, this view will show the user the history of a football team. E.g " the team was established in 1990" etc and give a full history of the team. So there will be no user interaction. The reader will only read the text and move on to the next page. Is using a textbox for the text a good idea? And is there anyway to justify the alignment of the text so the ends of the lines are all alligned?
I think You will Use WebView Because justified aligment for text you only have center, left and right by UItextView.
IF You Want to Use UiWebView then set in
className.h
#interface className : UIViewController {
IBOutlet UIWebView *webviewName;
}
#property (nonatomic, retain) UIWebView *webviewName;
className.m
[webviewName loadHTMLString:[NSString stringWithFormat:#"<div align='justify'>%#<div>",TEXT_set] baseURL:nil];
UITextView is a good way to go, but it has no option to justify the text it contains. Another approach is using a UIWebView to display a simple HTML document.
To have nice looking justified text in HTML set the text-align property to justify and may use some JavaScript to enhance line-breaking.
Hope that helps :)
ok there is no problem with that u shoud to use textView ,just make sure its user intteraction is disable, while if ur data is coming dynamic than u should to use Web view, this make alignment correct
I have found that a generic view controller subclass to load a web view is very reusable. I have two. One designed for a navigation controller, and another designed a a modal view controller. I use them over and again with modification for things like help, legal information, and about my company. It would work very well for your purposes.
I use KempoZer as my free html editor to create the files I load in it. The advantage is that the html editor allows me to add bold, italic, headings, and different font sizes.
In storyboard I have several segues to the same view controller and set the two properties in a prepare for segue method in the view controller that calls the html view controller.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"instructions"]) {
[[segue destinationViewController] setNavigationTitle:#"Instructions"];
[[segue destinationViewController] setHtmlFileName:#""];
} else if ([[segue identifier] isEqualToString:#"legal"]) {
[[segue destinationViewController] setNavigationTitle:#"Legal Notices"];
[[segue destinationViewController] setHtmlFileName:#"legal.html"];
} else if ([[segue identifier] isEqualToString:#"about"]) {
[[segue destinationViewController] setNavigationTitle:#"About"];
[[segue destinationViewController] setHtmlFileName:#"AboutSuperDuperCoolApps.html"];
} else if ([[segue identifier] isEqualToString:#"videoTutorials"]) {
[[segue destinationViewController] setNavigationTitle:#"Video Tutorials"];
[[segue destinationViewController] setHtmlFileName:#""];
}
}
Here is the nab controller version, don't forget to create the storyboard view controller and hook up the web view.
// HTMLViewController.h
#import <UIKit/UIKit.h>
#interface HTMLViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIWebView *webView;
#property (strong, nonatomic) NSString *htmlFileName;
#property (strong, nonatomic) NSString *navigationTitle;
#end
and
// HTMLViewController.
#import "HTMLViewController.h"
#implementation HTMLViewController
#synthesize webView, htmlFileName, navigationTitle;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (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
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
//
[self.navigationItem setTitle:navigationTitle];
// load selected html file
NSString *bundle = [[NSBundle mainBundle] bundlePath];
NSString *webPath = [bundle stringByAppendingPathComponent:htmlFileName];
[super viewDidLoad];
[webView loadRequest:[NSURLRequest requestWithURL:
[NSURL fileURLWithPath:webPath]]];
//
[super viewDidLoad];
}
- (void)viewDidUnload
{
[self setWebView:nil];
[self setHtmlFileName:nil];
[self setNavigationTitle:nil];
[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
Related
I have a navigation controller with multiple cells. I want to link each of the cells to a webpage in a web view. However, I don't want to create another view controller for each cell. Is it possible to create 1 view controller and link all cells to the same view controller but make a different webpage show up in the web view for each cell? For example, there is cell 1 and cell 2. They both link to one view controller with a web view in the view controller. When you click cell 1, it brings you to the view controller and in the web view it brings you to http://example.com. When you click cell 2, it brings you to the same view controller as cell 1 but in the web view, it brings you to http://example.com/1. How can I do that?
Just declare a property in your viewcontroller which holds the URL you want to request.
#property (strong,nonatomic) NSString *url;
When you click on a cell assign the URL of the cell accordingly to the property of your viewcontroller
Your viewcontroller should look something like this.
Header
#import <UIKit/UIKit.h>
#interface RSViewController : UIViewController
#property (weak, nonatomic) NSString *url;
#end
Implementation
#import "RSViewController.h"
#interface RSViewController ()<UIWebViewDelegate>
#property (strong,nonatomic) IBOutlet UIWebView *webview;
#end
#implementation RSViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.webview.delegate = self;
NSURLRequest *urlRequest = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:self.url]];
[self.webview loadRequest:urlRequest];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(#"Did start loading");
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(#"Did finish loading");
}
#end
Now you just need a reference to your viewcontroller in your tableviewcontrollerand set the urlproperty on cell click.
I have a written a simple example where I have a Utility Application project with one UIScrollView on it. When I click the info button to flip the screen and return the UIScrollView now is unresponsive. Not only that but I purposely placed the scroller in the Interface Builder in the upper left corner and then programmatically set it in the center. when I come back from the flipside its shifted up to the top left corner and unresponsive. why?
This is my .h file:
#import "POCFlipsideViewController.h"
#interface POCMainViewController : UIViewController <POCFlipsideViewControllerDelegate, UIScrollViewDelegate>
#property (nonatomic,retain) IBOutlet UIScrollView *scroller;
#end
This is my .m file
#import "POCMainViewController.h"
#interface POCMainViewController ()
#end
#implementation POCMainViewController
#synthesize scroller =_scroller;
- (void)viewDidAppear:(BOOL)animated
{
[_scroller setContentSize:CGSizeMake(80.0f, 320.0f)];
[_scroller setFrame:CGRectMake(120, 131, 80, 214)];
[super viewDidAppear:animated];
}
- (void)viewDidLoad
{
_scroller.delegate = self;
_scroller.showsVerticalScrollIndicator = NO;
_scroller.decelerationRate = UIScrollViewDecelerationRateFast;
[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 - Flipside View
- (void)flipsideViewControllerDidFinish:(POCFlipsideViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showAlternate"]) {
[[segue destinationViewController] setDelegate:self];
}
}
#end
The rest is boiler plate straight from Xcode.
I have found that the culprit with UIScrollView is often Auto Layout. In Storyboard, highlight the view element under Main View Controller and then open the "Show the File Inspector" window in the right-hand column. Under "Interface Builder Document", deselect "Use Auto Layout" and see if that corrects your issue.
I'm currently learning iPhone applications development, and I made some online tutorials to learn how all this is working.
I'm now quite used to the Objective-C concepts, and I'm trying to build a first application based on two views :
The first view would be the "Login view", simply with a kind of login system : a login field and a password field, and a "connect" button.
The second view is the "Home view" of the application, which will be called after the login.
I made a push segue to make the relation between the Login view and the view that is called after login. Here's what the storyboard looks like :
What I don't know actually is how to call a function that will check if the credentials are correct, and the switch to the other view if the login succeed.
Can anyone explain me, or give me some tips / tutorials for this please ?
Here are the sources for my LoginController :
LoginController.h
#interface LoginController : UIViewController {
IBOutlet UITextField *TFLogin;
IBOutlet UITextField *TFPassword;
}
#property (strong, nonatomic) IBOutlet UITextField *TFLogin;
#property (strong, nonatomic) IBOutlet UITextField *TFPassword;
- (IBAction)Connect:(UIButton *)sender;
#end
LoginController.m
#implementation LoginController
#synthesize TFLogin;
#synthesize TFPassword;
- (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)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)Connect:(UIButton *)sender
{
if ([TFLogin.text isEqualToString:#"myLogin"] && [TFPassword.text isEqualToString:#"myPassword"]) {
[self performSegueWithIdentifier:#"LoginSegue" sender:sender];
NSLog(#"Connection OK");
}
else {
NSLog(#"Connection Not OK");
}
}
#end
Thanks !
You have two choices for triggering a segue. The easy way is just to ctrl-drag in interface builder from the button to the next view controller. You can also do it in code (in an IBAction), by calling performSegueWithIdentifier:sender:.
If you go with the IBAction, you can validate the data there.
If you go with the interface builder method, you can't validate -- prepareForSegue:sender: will be too late. Anyway, there's a possible stumbling block here -- as I recall, UINavigationController doesn't forward prepareForSegue:sender: to its children. You can mitigate this with a category on UINavigationController or by subclassing.
I am creating my first tab controller app. I have 2 tabs with 2 UIViews in them. I did this mostly from Interface Builder all I did in Xcode was add 2 files firstControllerView and SecController view. I can see the tab controller is working went I run the app (I simply changed the background color on the 2 UIViews in the tabs to see the effect).
Now I want to add a label to the secondView and set its text programmatically from code. This is whats breaking for me! I am doing something wrong. In my SecondViewController.h it looks like this:
#interface SecondViewController : UIViewController {
IBOutlet UILabel *title;
}
#property (nonatomic,retain) UILabel *title;
#end
and the .m looks like this...
#import "SecondViewController.h"
#implementation SecondViewController
#synthesize title;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[title setText:#"Hello Nick"];
[super viewDidLoad];
}
- (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 {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[title release];
[super dealloc];
}
#end
After this I went back to Interface Builder and dragged the outlet reference to the label. When I run the simulator it crashes.
What am I missing here? It must be something simple.
Forgot to create an outlet for a tabbarcontroller in the app delegate then connect that outlet to the tabbar controller in interface builder.
I want to know how to implement an activityIndicator in a WebView based app, I wrote the following code but the indicator does not appear.
The webview load file locally, so it load very fast, but when it load an external page it load slow and I need the indicator...
FirstViewController.h
#import <UIKit/UIKit.h>
#interface FirstViewController :
UIViewController <UIWebViewDelegate>{
IBOutlet UIWebView *webview1;
NSURL *urlLocation;
IBOutlet UIActivityIndicatorView *m_activity;
}
#property (nonatomic, retain) UIActivityIndicatorView *m_activity;
- (IBAction)searchbutton:(id)sender;
- (IBAction)home:(id)sender;
#end
FirstViewController.m
#import "FirstViewController.h"
#implementation FirstViewController
#synthesize m_activity;
// viewWillAppear loads every time younopen up this View
- (void)viewWillAppear:(BOOL)animated {
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
urlLocation = [NSURL fileURLWithPath:filePath];
[webview1 loadRequest:[NSURLRequest requestWithURL:urlLocation]];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
//Initialization code
m_activity = nil;
}
return self;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
m_activity.hidden= TRUE;
[m_activity stopAnimating];
NSLog(#"Web View started loading...");
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
m_activity.hidden= FALSE;
[m_activity startAnimating];
NSLog(#"Web View Did finish loading");
}
Why are you setting your activity indicator to nil in your init?
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
//Initialization code
m_activity = nil;
}
return self;
}
The call to super initialized your indicator from your XIB (assuming you connected it to your outlet in IB), but then you are setting the reference to nil after it's been initialized. Remove that line. Then go back into interface builder and set the "Hide when stopped" checkbox. Now you can simplify your code that displays the indicator:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[m_activity stopAnimating];
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
[m_activity startAnimating];
}
The "Hide when stopped" causes the indicator to hide when you stop it from animating.
Whats the issue here, the code you posted above should work, except that you dont initialize the indicator anywhere (maybe you do in viewDidLoad) but the code shown above should work given that the indicator was initialized correctly and u set the webview d elegate to the view controller there, I have it working on some of my apps where i use webviews and indicators to indicate when its loading...
UIWebView.loading property can also be used.
Apple's doc:
#property(nonatomic, readonly, getter=isLoading) BOOL loading
Description
A Boolean value indicating whether the receiver is done loading content. (read-only)
If YES, the receiver is still loading content; otherwise, NO.
In iOS6 it looks like Apple has fixed some issues with this property as well. http://code-gotcha.blogspot.fi/2012/08/uiwebviewloading-in-ios-6-fixed.html