to all im newbie developer and trying to create my first iPhone app, sorry my english not good
i trying to make 2 views and the second view display over first view, but the second view is not full screen, is 320x400 i cut 80pixels, to see the buttons from the first view... and i making that with tutorials from one book, but in book he making 1 button on first view and 1 another button in the second view... and i need 2 buttons on the first view, and can't make it. please help me... i think is very easy but i dont know how to..
with this code make in book what i reading
**FirstView.h**
#import UIKit/UIKit.h
#interface FirstView : UIViewController {
}
/--- declare an action for the Button view ---/
-(IBAction) goToSecondView:(id) sender;
#end
**FirstView.m**
#import "FirstView.h"
#import "SecondView.h"
#implementation FirstView
SecondView *secondView;
-(IBAction) goToSecondView:(id) sender{
secondView = [[SecondView alloc] initWithNibName:nil bundle:nil];
[self.view addSubview:secondView.view];
}
-(void)dealloc {
[SecondView release];
[super dealloc];
}
**SecondView.h**
#import
#interface SecondView : UIViewController {
}
/*--- action for the Return button --- */
-(IBAction) goToFirstView:(id) sender;
#end
**SecondView.m**
#import "SecondView.h"
#implementation SecondView
-(IBAction) goToFirstView:(id) sender {
[self.view removeFromSuperview];}
And after linking on Interface Builder one button (goToSecondView) on the first view, and one button on the SecondView(goToFirstView).... and i want to make this two buttons on the first view.
Help me please! , thank you.
Just put another button in the first view and move your code of goToFirstView to the FirstView controller. Then, change where you coded [self.view removeFromSuperview]; by a [secondView.view removeFromSuperview];
I suggest you to read some tutorials of how the views and viewcontrollers are designed in iPhone.
Related
I'm looking to create a sizeable subview that's draggable like this:
If there is an IBAction that takes you to the next View (SecondViewController) and when it does, there's another IBAction there and when you click on that one, it creates a SubView that's about half of the size of the current screen you're in (SecondViewController) that shows the third view controller that would be created? Also, how would you make that subView draggable? Thank you for helping.
Sorry, just to be clear, you want your second view controller to have a button that when tapped, adds your third view controller taking up half the screen at the bottom?
If this is the case then you can do this with the new view controller containers in iOS5.
Ok, so you have three view controllers. For the sake of this lets say your class are called FirstViewController, SecondViewController and ThirdViewController.
I assume from what you say that you already have you instance of FirstViewController with a button, that moves you on to an instance of SecondViewController, and that the issue is then getting SecondViewController to add an instance of ThirdViewController to the bottom half of the screen when a button is pressed.
The .m file for SecondViewController will need to do something like this:
#import "ThirdViewController.h"
#interface SecondViewController ()
#property (retain) ThirdViewController *thirdViewConroller;
- (void)buttonTap;
#end
#implementation SecondViewController
#synthesize thirdViewConroller = _thirdViewConroller;
- (void)dealloc {
self.thirdViewConroller = nil;
[super dealloc];
}
- (void)loadView {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.titleLabel.text = #"Show third controller";
[button addTarget:self action:#selector(buttonTap) forControlEvents:UIControlEventTouchUpInside];
button.frame = // Some CGRect of where you want the button to be
[self.view addSubview:button];
}
- (void)buttonTap {
// When the button is tapped, create an instance of your ThirdViewController and add it
self.thirdViewConroller = [[ThirdViewController alloc] initWithFrame:/* Some CGRect where you want the controller to be */ ];
[self.thirdViewConroller willMoveToParentViewController:self];
[self addChildViewController:self.thirdViewConroller];
[self.thirdViewConroller didMoveToParentViewController:self];
}
#end
This should give you a button on your second controller, that will create and add you third controller. Make sure yo us till have all the standard methods that you had before, this should be in addition to what you have.
In your interface for ThirdViewController:
#interface ThirdViewController : UIViewController <NSObject>
- (id)initWithFrame:(CGRect)frame;
#end
Then in the implementation of your ThirdViewController:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithNibName:nil bundle:nil];
if (self) {
self.view.frame = frame;
// Do your init stuff here
}
return self;
}
It should then handle adding the views and such forth.
Make sure your thirdViewController class has a valid initWithFrame: initialiser method.
This should do the trick, if you need any further help let me know :)
I am starting a multiview app with two views: NewGame and Players. I thought I was setting everything up properly, but apparently not.
MainViewController.h
#import <UIKit/UIKit.h>
#class NewGame; #class Players;
#interface MainViewController : UIViewController {
IBOutlet NewGame *newGameController;
IBOutlet Players *playersController;
}
-(IBAction) loadNewGame:(id)sender;
-(IBAction) loadPlayers:(id)sender;
-(void) clearView;
#end
MainViewController.m
#import "MainViewController.h"
#import "NewGame.h"
#import "Players.h"
#implementation MainViewController
-(IBAction) loadNewGame:(id)sender {
[self clearView];
[self.view insertSubview:newGameController atIndex:0];
}
-(IBAction) loadPlayers:(id)sender {
[self clearView];
[self.view insertSubview:playersController atIndex:0];
}
-(void) clearView {
if (newGameController.view.superview) {
[newGameController.view removeFromSuperview];
} else if (playersController.view.superview) {
[playersController.view removeFromSuperview];
}
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[self loadNewGame:nil];
[super viewDidLoad];
}
A couple of images...
http://i.stack.imgur.com/GwXMa.png
http://i.stack.imgur.com/XHktH.png
Views are objects that represent what appears on screen. View controllers are objects that perform application logic relating to those views. A view hierarchy is a collection of views. You are attempting to add a view controller to a view hierarchy as if it were actually a view.
Roughly speaking, you should have one view controller for every "screen" of your app. This view controller can manage any number of views. Its main view is accessible through its view property.
A quick fix to get your application operational would be to add the main view of your view controllers instead of the view controllers themselves. So, for example, this:
[self.view insertSubview:playersController atIndex:0];
...would become this:
[self.view insertSubview:playersController.view atIndex:0];
Having said that, this is not a good solution long-term, and you should investigate a more structured way of organising transitions from view controller to view controller. UINavigationController is a good option for beginners.
I suppose playerController is view controller. Then add
[self.view addSubview: playerController.view];
OR if not then subclass them for UIView
NewGame and Players both need to subclass UIView. If they're ViewControllers, not UIViews, you'll need to use newGameController.view instead of newGameController.
I am trying to implement a modal navigation controller as described in the Apple iOs Guide: Combined View Controller Interfaces
I have come to the conclusion that I am missing something both obvious and stupid as I simply cannot get anything to display, I get a blank white screen.
Swapping things out I can prove that the view controller that I am using as the navigation controllers RootViewController works fine on it's own (by adding it manually as a view subChild).
Further, implementing addSubView ([self.view addSubview:navController.view]) instead of presentModalViewController seems to work OK.
Can anyone point out my simple error because I am 5 minutes short of kicking my own face :D
header
#import <UIKit/UIKit.h>
#interface BaseViewController : UIViewController {
}
implementation
#import "BaseViewController.h"
#import "ScannedListViewController.h"
#import "ScannedItemViewController.h"
#implementation BaseViewController
- (void)viewDidLoad {
ScannedListViewController *listViewController = [[ScannedListViewController alloc] init];
ScannedItemViewController *itemViewController = [[ScannedItemViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:listViewController];
[navController pushViewController:itemViewController animated:NO];
[self presentModalViewController:navController animated:YES];
[listViewController release];
[itemViewController release];
[navController release];
[super viewDidLoad];
}
The RootControllerView is a basic test TableViewController with the following header
#interface ScannedListViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>
Thank you in advance if your able to help
Why are you presenting something modally in a view controller's viewDidLoad method? I find that odd off the top. Generally you show a modal view controller in response to some action (like tapping a button).
Is there a reason you're showing a navigation controller with a second view controller already pushed on it after the root?
You should have [super viewDidLoad] as the first line, not the last line, of the method.
You do not need to have <UITableViewDelegate, UITableViewDataSource> after UITableViewController because it already adopts those protocols. Remove that bit.
im newbie developer and creating my first iphone app... and i have one little problem :)
i switching in my program 2 views, secondView is over firstView, and when i press 2 times or more on button to show the SecondView iphone simulator stopping worling and if after i press to show the FirstView he still showing SecondView view :(...
and i need help how to make button to pressing one time only, and if after switch back to FirstView to can again press one time,and shows like presse,now it show pressed only when i tuch it,... i want like buttons in TabBar, and if i use the TabBar is more harder for me i dont know how to resize it to height and add custom background, and change the effect of pushed button
Thanks you very much and sorry for my bad english!.
here is what code i use to switching views with buttons
// FirstView.h
#import <UIKit/UIKit.h>
#interface FirstView : UIViewController {
}
-(IBAction) goToSecondView:(id) sender;
-(IBAction) goToFirstView:(id) sender;
#end
// FirstView.m
#import "FirstView.h"
#import "SecondView.h"
#implementation FirstView
SecondView *secondView;
-(IBAction) goToSecondView:(id) sender{
secondView = [[SecondView alloc] initWithNibName:#"SecondView" bundle:nil];
[self.view addSubview:secondView.view];
}
-(IBAction) goToFirstView:(id) sender {
[secondView.view removeFromSuperview];
}
thank you very much!
This:
#implementation FirstView
SecondView *secondView;
... is most likely the source of your crash. You shouldn't define instance variables in the implementation. The compiler may allow it but the runtime will be confused and the instance variable will not be properly retained.
You should define it like:
#interface FirstView : UIViewController {
SecondView *secondView;
}
#property(nonatomic, retain) SecondView *secondView;
...and use it like:
-(IBAction) goToSecondView:(id) sender{
UIView *newView = [[SecondView alloc] initWithNibName:#"SecondView" bundle:nil];
self.secondView=newView;
[newView release];
[self.view addSubview:self.secondView.view];
}
For clarity you should also rename FirstView and SecondView to FirstViewController and SecondViewController because they are view controllers and not views themselves.
More generally, what you are trying to do is dangerous and difficult. You don't swap views by adding and removing them as subviews. You need to swap out view controller and their views using a UINavigationController or a UITabbarController. In Xcode File>New Project, there is a Navigation based project and a Tabbar based project templates. Either will provide you most of the code you need to implement a simple app using either controller.
It will be well worth your time to spend a day learning how to use these controllers properly. With your current design, your app will break if it gets much more than two views.
here is a iPhone programming beginner's question:
How do I get to another view by pressing a button in my main view?
I have the following function which is executed when I press a button, and debugging it, he passes there, but my "Warning" view does not show up:
-(IBAction) showWarningView:(id)sender
{
if(self.showWarning == nil){
WarningViewController *nextView = [[WarningViewController alloc] initWithNibName:#"Warning" bundle:[NSBundle mainBundle]];
self.showWarning = nextView;
[nextView release];
}
[self.navigationController pushViewController:self.showWarning animated:YES];
}
My main RootViewController looks like this:
#import <UIKit/UIKit.h>
#import "WarningViewController.h"
#interface RootViewController : UIViewController {
IBOutlet UIButton *button1;
WarningViewController *showWarning;
}
#property(nonatomic,retain) WarningViewController *showWarning;
-(IBAction) showWarningView:(id)sender;
#end
I am using the navigation control of a UITableViewController but what do I have to use to just simply show my other view when I press a button in a view-based application?
Thanks a lot!
edited
if you're using the navigation control of a UITableViewController, you probably have to push the view in you tableviewcontrollers navigation controller
this means you have pass the navigation controller of your tableviewcontroller on to your viewcontroller, then you just push it
e.g.
[self.tableViewControllersNavigationController pushViewController:self.showWarning animated:YES];
(for passing the tableViewController's navigationController on, you might have to create a delegate pattern)
Does the title in your navigation bar change? Maybe you don't have a UIView associated with your UIViewController inside your Interface Builder file.