Content moved after when back to first viewcontroller - objective-c

I have 2 viewcontrollers in a navigationController.
In the first viewcontroller I have a UIButton that open the second viewcontroller.
When second viewController is load I can see the "back" button on top-left of the screen.
Now, when I come back to the first viewController the content is some points upper than last time.
Like the next image http://imperiosweb.com/tmp/sample.png
Now, first ViewController has a UIScrollView that has the content.
FirstViewController.h
...
#interface FirstViewController : UIViewController{
IBOutlet UIScrollView *Scroller;
}
#property (weak, nonatomic) IBOutlet UIScrollView *Scroller;
...
FirstViewController.m
...
#implementation FirstViewController
...
#synthesize Scroller = _scroller;
...
- (void)viewDidLoad
{
[self.Scroller setBounces:YES];
...
}
I don't know if is the center of the scrollView or something else, I was trying to set center, works but doesn't repair the problem (content moved)
- (void)viewDidAppear:(BOOL)animated {
[self.view setCenter:self.Scroller.center];
self.Scroller.frame = CGRectMake(0,0,320,416);
self.Scroller.contentSize = CGSizeMake(320, (550 + self.Scroller.contentOffset.y));
[self.Scroller setCenter:(CGPointMake(160.0,208.0))];
}
What can I do to fix content like first time (App just launched)?
Thanks!

Related

Changing views on a window with a button click

what I'm basically trying to make is a very simple program that can switch back and forth between 2 views on a single window.
When the program loads there is a window with a custom view that contains a login button. When clicked, the view changes to a second custom view that contains a label and a logout button. I have this much working.
What I can't figure out is how to get the logout button to bring me back to the first view.
Here is the code for the AppDelegate class where i have the button method to switch the view:
header:
#import <Cocoa/Cocoa.h>
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (weak) IBOutlet NSWindow *window;
#property (weak) IBOutlet NSView *loginView;
- (IBAction)loginButtonClicked:(id)sender;
#end
implementation:
#import "AppDelegate.h"
#import "myCustomView.h"
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
- (IBAction)loginButtonClicked:(id)sender {
[_loginView removeFromSuperview];
myCustomView *new = [[myCustomView alloc]initWithNibName:#"myCustomView" bundle:nil];
[[[self window]contentView]addSubview:[new view]];
}
#end
This is the code for my custom class that is a subclass of NSViewController.
header:
#import <Cocoa/Cocoa.h>
#class AppDelegate;
#interface myCustomView : NSViewController
#property (strong) IBOutlet NSView *logoutView;
- (IBAction)logoutButtonClicked:(id)sender;
#end
implementation:
#import "myCustomView.h"
#implementation myCustomView
- (void)viewDidLoad {
[super viewDidLoad];
// Do view setup here.
}
- (IBAction)logoutButtonClicked:(id)sender {
[_logoutView removeFromSuperview];
myCustomView *newController = [[myCustomView alloc]initWithNibName:#"MainMenu" bundle:nil];
[[[self window]contentView]addSubView:[newController view]];
//this does not work. No visible #interface for 'myCustomClass' declares the selector 'window'
}
#end
The button method to go back to the login page is where I'm stuck. Even though I've added the header file for AppDelegate into myCustomClass I cannot use the instance of NSWindow. What am I doing wrong here? Am I at least on the right track? any help here is greatly appreciated.
I also tried using #class instead of #import, but still can't use the instance of NSWindow from AppDelegate.
Here are the pictures of my two xib files:
[][1
UPDATE: The suggestions from Paul Patterson in his comments were very helpful, but haven't solved my problem. For now what I am doing to get my project to work is putting the buttons in the window instead of the views and then hiding them when i don't need them. This works and I can switch back and forth, however I still can't figure out how to use a button on a custom view itself to load a different view onto the same window.

Unable to implement simple view scrolling in iOS

I'm having a hard time implementing a simple scroll on my detail view.
The app is straightforward with a Master and Detail views.
When the user taps an item on Master, the Detail view is pushed with larger photo, blog text and etc.
I would like the entire Detail view to scroll, so if the picture is tall, or the text is long, they can scroll vertically to see/read more. I do not want the user to scroll these items individually. It should feel like webpage scrolling.
Currently my Detail view loads OK but I can't make it scroll.
My DetailViewController.h
#import <UIKit/UIKit.h>
#interface DetailViewController : UIViewController {
IBOutlet UILabel *postTextLabel; // wired to Text Label
IBOutlet UILabel *postAuthorNameLabel; // wired to Author Label
}
#property (strong, nonatomic) id detailItem;
#end
My DetailViewController.m
#import "DetailViewController.h"
#interface DetailViewController ()
- (void)configureView;
#end
#implementation DetailViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self configureView];
}
- (void)configureView
{
if (self.detailItem) {
NSDictionary *post = self.detailItem;
NSString *postText = [post objectForKey:#"post_text"];
NSString *postAuthorName = [post objectForKey:#"post_author_name"];
postTextLabel.text = postText;
postAuthorNameLabel.text = postAuthorName;
}
}
#end
Structure on IB:
Any ideas on what's missing to make this work?
I would do the following:
1)(optional)turn your View into a scrollview by dragging it into the view in the scruture list on the side.
2)link the scrollView into your viewcontroller .h and make an Outlet connection something like this
#property (strong, nonatomic) IBOutlet UIScrollView *scroller;
(make sure you add #synthesize in the .m if you add this manually)
and make sure it is connected in IB!
3)set the contentsize of the scrollview in viewDidLoad method
scroller.contentSize = CGSizeMake(320,550);
NOTE: IBOutlet UILabel *postTextLabel; should actually probably be a UITextView so you can access ContentSize
Which would allow for the following.
CGRect frame = postTextLabel.frame;
frame.size = postTextLabel.contentSize;
postTextLabel.frame = frame;
scroller.contentSize = CGSizeMake(320, frame.size.height+200);//200 or how ever much space is above the textView
and again that only works if you use a UITextView
for ipad replace 320 with 768 or 1024, or whichever depending on orientation
The best way to connect it in IB is like this holding down control and dragging to the .h file
and make sure it is set to automatic like in the picture, and pointing to> the .h of your view.
Adding it like this also automatically adds the #synthesize for you.
Make sure UserInteractionsEnabled are checked here for our scrollview

Change ViewControllers & Pass Data in iOS

I'm trying to have my app take a picture then pass the image onto another view for editing but I can't seem to figure out how to change views, how to add an "ID" to a view in the storyboard or how to pass data between views.
The communication between two UIViewControllers needs to be managed manually, however, if you are using storyboards to create your app, there's some things that you need to take into account.
Let's say you have FirstViewController and SecondViewController(Lets assume you have everything set up in your Storyboard). FirstViewController will pass a UIImage to SecondViewController and they will look something like this.
#interface FirstViewController : UIViewController
- (IBAction)transitionToNextViewController;
#property (retain, nonatomic) UIImage *image;
#end
#implementation FirstViewContoller
- (IBAction)transitionToNextViewController;
{
[self performSegueWithIdentifier:#"SegueIdentifier"];
}
#end
And:
#interface SecondViewController : UIViewController
#property (retain, nonatomic) UIImage *image;
#end
You are probably wondering how are you supposed to pass the image to the SecondViewController. Well, when using storyboards, your UIViewControllers will receive a call to their method prepareForSegue:sender: . All you have to do is set the image property for the second UIViewController there.
#implementation FirstViewController
- (IBAction)transitionToNextViewController;
{
[self performSegueWithIdentifier:#"SegueIdentifier"];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
SecondViewController *secondViewController = (SecondViewController *)segue.destinationViewController; // You have to cast it
secondViewController.image = self.image;
}
#end
and that's it. To better understand Storyboards please read the apple docs here.

Obejct-C / Xcode Not able to add/change text in UITextView

For some reason, i'm not able to change or add text in my UITextView from code.
I have made Outlet and connected (All in IB) - nothing happens.
Even tried to add -setNeedsDisplay
Do i need to set some property ? - This i driving me nuts....
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITextViewDelegate> {
UITextView *textInfoView;
}
#property (nonatomic, retain) IBOutlet UITextView *textInfoView;
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
textInfoView.text = #"Test";
}
-(void)viewDidAppear:(BOOL)animated{
textInfoView.text = #"Test";
textInfoView.text = [textInfoView.text stringByAppendingString:#"Line2"];
[textInfoView setNeedsDisplay];
It seems, really strange that this code doesn't update the text inside the UITextView. I have tried and it works for me ... I think that you don't have correctly linked the Outlet in interface builder, or you don't have associated your view controller to your view ...
UPS - Got it...
Forgot to #synthesize...
For some reason i did not get warning or "setter error" in Xcode 4.4, maybe it's a version bug ?

Correct way of setting up UINavigationController and its RootViewController in IB

I have the following structure in my iPad App:
Application
UINavigationController (Providing the top bar with UIBarButtons etc.)
Initial Login screen
Second login screen
I am not sure how I should now set this up in Interfacebuilder correctly. My guess would be that I create two ViewControllers:
LoginVC1: (This one should also include the NavigationController since it is the first of the two screens)
LoginVC2: Based on some delegate callback from LoginVC1 my application would push to this ViewController.
This is my LoginVC1 in IB:
LoginVC1 http://k.minus.com/jpamEAFkBjpKT.png
And when I present it modally it looks like this which is not what I want:
Result http://k.minus.com/jHAYRnY788jFt.png
The result:
Nor the title of the ViewController nor the Cancel button is shown
The view seems to be empty despite my view in IB
I have set the Presentation mode of the UINavigationController to FormSheet which is ignored too since it is displayed in fullscreen.
What am I doing wrong?
Kjuly I simply do presentModalViewController to show the dialog, but
since my UINavigationControler contains a UIViewController (See
nib-screen), I would assume that this ViewController is displayed when
the modal is shown.
Not always Besi. Adding it to the UINavigationController Hierarchy doesn't add is as the rootViewController. That must be done in code like this:
UIViewController *rootViewController = [[[ExamplesViewController alloc] init] autorelease];
UINavigationController * navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
Also, check that you are presenting the NavigationController, and not the ViewController modally.
To solve the NavigationBar title issue, try setting the self.title property in the
-(void)viewDidload method and see if that works. If not, try self.//instance of UINavigationController//.navigationBar.title = #"string".
As for the other buttons not showing up, my guess is that if setting the root controller in code doesn't help, then you only made reference to them in the .h, instead of instantiating them. So either call something like this in the .h:
//.h
#implementation ExampleViewController: UIViewController <UITextFieldDelegate> {
IBOutlet UIBarButtonItem * CancelButton;
IBOutlet UITextField * usernameField;
IBOutlet UITextField * passwordField;
IBOutlet UIButton * loginButton;
}
#property (nonatomic, retain) IBOutlet UIBarButtonItem * CancelButton;
#property (nonatomic, retain) IBOutlet UITextField * usernameField;
#property (nonatomic, retain) IBOutlet UITextField * passwordField;
#property (nonatomic, retain) IBOutlet UIButton * loginButton;
Then connect the outlets in the XIB, or instantiate the buttons in the .m with:
//.m
-(void)viewDidLoad {
//do stuff
CancelButton = [[[UIBarButtonItem alloc]initWithTitle:#"Cancel" style:UIBarButtonItemStyleDone target:self action:#selector(dismissSelf)]autorelease];
//do more stuff
}