UIScrollView unresponsive after returning from FlipsideViewController - iphone

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.

Related

Connecting Touch Up Inside event to File's Owner

I’m working through the Multiview Applications chapter of Apress’s Beginning iOS5 development and ran into an issue making a connection from a button on one of the views to File Owner. The general premise of the program is to use a root controller, BIDSwitchViewController to switch between two content views BIDBlueViewController and BIDYellowViewController. Each of the content views has a single button connecting the Touch Up Inside event to the File’s Owner. I had no problem making the connection in BIDBlueViewController, but when I attempt to make this connection in the BIDYellowViewController I’m unable to make a connection between the Touch Up Inside event and the File’s Owner icon.
The instructions in the book state
Drag a Round Rect Button from the library over to the view, using the
guidelines to center the button in the view, both vertically and
horizontally. Double-click the button, and change its title to Press
Me. Next, with the button still selected, switch to the connections
inspector drag from the Touch Up Inside event to the
File's Owner icon, and connect to the blueButtonPressed action method
I’m able to do this without any issues, however when I try complete this instruction for the Yellow View
Drag a Round Rect Button from the library over to the view, using the
guidelines to center the button in the view, both vertically and
horizontally. Double-click the button, and change its title to Press
Me. Next, with the button still selected, switch to the connections
inspector drag from the Touch Up Inside event to the
File's Owner icon, and connect to the yellowButtonPressed action method
The File Owner icon will not let me make the connection, as I drag from the Touch Up Inside event to the File Owner icon File Owner never highlights and I cannot make the connection.
Here is my code
The root controller interface
//
// BIDSwitchViewController.h
// ViewSwitcher
//
//
#import <UIKit/UIKit.h>
#class BIDYellowViewController;
#class BIDBlueViewController;
#interface BIDSwitchViewController : UIViewController
#property (strong, nonatomic) BIDYellowViewController *yellowViewController;
#property (strong, nonatomic) BIDBlueViewController *blueViewController;
- (IBAction)switchViews:(id)sender;
#end
The root controller implementation
//
// BIDSwitchViewController.m
// ViewSwitcher
//
#import "BIDSwitchViewController.h"
#import "BIDYellowViewController.h"
#import "BIDBlueViewController.h"
#implementation BIDSwitchViewController
#synthesize yellowViewController;
#synthesize blueViewController;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
self.blueViewController = [[BIDBlueViewController alloc]
initWithNibName:#"BlueView" bundle:nil];
[self.view insertSubview:self.blueViewController.view atIndex:0];
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)switchViews:(id)sender{
if (self.yellowViewController.view.superview == nil){
if (self.yellowViewController == nil) {
self.yellowViewController =
[[BIDYellowViewController alloc] initWithNibName: #"YellowView"
bundle:nil];
}
[blueViewController.view removeFromSuperview];
[self.view insertSubview:self.yellowViewController.view atIndex:0];
} else {
if (self.blueViewController == nil){
self.blueViewController = [[BIDBlueViewController alloc] initWithNibName:#"BlueView"
bundle:nil];
}
[yellowViewController.view removeFromSuperview];
[self.view insertSubview:self.blueViewController.view atIndex: 0];
}
}
- (void) didReceiveMemoryWarning {
// Release the view if it doesn't have a super view
[super didReceiveMemoryWarning];
// Release any cached data, images, etc, that aren't in use
if (self.blueViewController.view.superview == nil){
self.blueViewController = nil;
} else {
self.yellowViewController = nil;
}
}
#end
The blue content view controller interface
//
// BIDSwitchViewController.h
// ViewSwitcher
//
#import <UIKit/UIKit.h>
#class BIDYellowViewController;
#class BIDBlueViewController;
#interface BIDSwitchViewController : UIViewController
#property (strong, nonatomic) BIDYellowViewController *yellowViewController;
#property (strong, nonatomic) BIDBlueViewController *blueViewController;
- (IBAction)switchViews:(id)sender;
#end
The blue content view controller implementation
//
// BIDBlueViewController.m
// ViewSwitcher
//
//
#import "BIDBlueViewController.h"
#interface BIDBlueViewController ()
#end
#implementation BIDBlueViewController
- (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);
}
#end
The yellow content view controller interface
//
// BIDYellowViewController.h
// ViewSwitcher
//
#import <UIKit/UIKit.h>
#interface BIDYellowViewController : UIViewController
- (IBAction)yellowButtonPressed;
#end
The yellow content view controller implementation
//
// BIDYellowViewController.m
// ViewSwitcher
//
//
#import "BIDYellowViewController.h"
#interface BIDYellowViewController ()
#end
#implementation BIDYellowViewController
- (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);
}
#end

How to justify text in a textbox ios?

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

EXEC_BAD_ACCESS in iOS5 for IBAction

I have written a sample code for button click action in xcode 4.2 for iOS5.
Here is the code
.h
#import <UIKit/UIKit.h>
#interface FirstViewController : UIViewController
#property(strong,nonatomic) IBOutlet UIButton *button;
-(IBAction)changed;
#end
.m
#import "FirstViewController.h"
#implementation FirstViewController
#synthesize button=_button;
- (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
- (void)viewDidLoad
{
[_button addTarget:self action:#selector(changed) forControlEvents:UIControlEventTouchUpInside];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
-(IBAction)changed
{
NSLog(#"clicked");
}
- (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 YES;
}
#end
But when I click the button. I am getting exception. how to solve this issue? the same is working in iOS 4.3
first change this code into this one
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[_button addTarget:self action:#selector(changed) forControlEvents:UIControlEventTouchUpInside];
}
I have found solution for my problem.
The main problem is that, if I create a view controller object locally (i.e., inside any method) and add it as subview then, when you invoke any IBAction, at that time it will raise an exception because, the memory for that viewController is getting deallocated automatically when declared locally.
I guess he needs to add data member for that button as well. I mean code should look like this.
#interface FirstViewController : UIViewController{
IBOutlet *UIButton *button;
}
#property(strong,nonatomic) IBOutlet UIButton *button;
-(IBAction)changed;
#end
and try replacing
#synthesize button = _button;
with
#synthesize button;
in your .m file.
You have the wrong message signature for your changed action. It should be:
- (IBAction)changed:(id)sender
and in the addTarget line of code:
#selector(changed:)
PS: Why are you using _button? I don't think this is related to the problem, but you should be using self.button instead. Accessing instance variables directly should be avoided, especially in this case, where you are allowing the compiler to decide what name the variable should have.
PPS: As mentioned by #InderKumarRathore, you should also be calling [super viewDidLoad] before running your own code.

Using UITouch To Drag A UITextField Around The Screen on iPhone

I am developing a sample application, where I have a situation for moving a UITextFields, UILabels etc., across the screen. I am not able to find any resource for implementing this.
I want to implement like this for a UITextField
Since you have an excellent tutorial on dragging already, I will assume your problem is the keyboard that comes up when you try to drag a UITextField instead of a UIView. The solution should be pretty simple: myTextField.userInteractionEnabled = NO; - that should disable user interaction and make it "read only". Perhaps have an edit mode where all text fields get this flag set. If it causes trouble, then add the textField to a UIView and then set the userInteractionEnabled to false. Then you can drag the UIView and it will drag the text field with it.
I followed Michael's suggestion and got the solution.I have given the code snippets below which will be useful for those who need to implement the same.
Steps:
Choose windows based application,then create a UIViewController subclass and add it to the window in the appdelegate file.
In the XIB of the viewcontroller class you created add UIViews and add the controls like textfield etc.,to the UIViews that you have created.We are going to move these views only,so add IBOutlets in the .h file of the view controller subclass and map them to the IB accordingly.
Sample Code
appdelegate.h
#import <UIKit/UIKit.h>
#import "MyView.h"
#interface MyAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
MyView *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
appdelegate.m
#import "MyAppDelegate.h"
#implementation MyAppDelegate
#synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch.
viewController=[[MyView alloc]init];
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
#end
viewcontroller.h
#import <UIKit/UIKit.h>
#interface MyView : UIViewController {
IBOutlet UIView *textFieldView;
IBOutlet UIView *labelView;
}
#end
viewcontroller.m
#import "MyView.h"
#implementation MyView
- (void)viewDidLoad {
[self.view addSubview:textFieldView];
[self.view addSubview:labelView];
[super viewDidLoad];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// get touch event
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if ([touch view] == textFieldView) {
// move the image view
textFieldView.center = touchLocation;
}
if ([touch view] == labelView) {
// move the image view
labelView.center = touchLocation;
}
}
- (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 {
[super dealloc];
}
#end
Thank u all.Have a niece time.

Connecting To a Label Within A UIView Of A Tab

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.