How we add the Subclass Xib in the Main class Xib? - iphone

I am working on application for Ipad.in this application i have a UIButton on my first class Xib ,Now i want to add second class XIB in this first Class XIB on Button click.i know the how to call second class XIB.which is just like this..
-(IBAction)displaysecondclass
{
secondview *sec=[[secondview alloc] initWithNibName:#"secondview" bundle:nil];
[self presentModalViewController:sec animated:YES];
[sec release];
}
But i not want to go secondclass ,i need to display second class XIB on firstview just like this.
-(IBAction)displaysecondclass
{
secondview *sec=[[secondview alloc] initWithNibName:#"secondview" bundle:nil];
[self.view addSubView:sec];
}
can any one help me how to add second class xib in first view.thanx in advance.

change the button code to
-(IBAction)displaysecondclass
{
secondview *sec=[[secondview alloc] initWithNibName:#"secondview" bundle:nil];
[self.view addSubView:sec.view];
}
Please note that you will need to add additional functions to release the viewcontroller once you dont need it anymore

Thanx for everyone actually i am working as part time IOS programmer.All though its was not difficult task That i mention in my quesiton.i was little bit confuse when i ask this question whether it is related about Subclass or newclass,thats why i post Question.Thanx for every one to help me.now i got its solution using this link may be it help to Some one new commer.
http://www.roseindia.net/tutorial/iphone/examples/iPhone-Create-SubView.html

Related

loading viewDidLoad at second xib issue

I have two xib files. I start with "ViewController.xib" and when the user clicks a button it loads a different xib file named "GamePage.xib".
The code of the button is:
-(IBAction)startButtonClicked{
UIViewController *gamePage = [[UIViewController alloc] initWithNibName:#"GamePage" bundle:nil];
[self presentViewController:gamePage animated:NO completion:nil];
}
When the button is clicked, the second xib is shown on screen, but its "viewDidLoad" method don't run…
Why is that?
UIViewController *gamePage = [[UIViewController alloc] initWithNibName:#"GamePage" bundle:nil];
This does not seem correct.
viewDidLoad is probably running but it is viewDidLoad of Apple's UIViewController. You should use your class instead of UIViewController when you initialize it. In general you should use the class which viewDidLoad you need called.
Your XIB must have its Owner. You need to create a class derived from UIViewController and then you need to make that class owner of your XIB.
Just follow these steps:
Open you project
Select the Project Name in the Left Navigation Panel
Right Click and select New File
Select Objective C Type Class
When you click on it, it will ask you to give it a name
Suppose you gave GameViewController
Check the "With XIB for Interface"
and then add the files
Now in your Navigation Panel you will see three classes
GameViewController.h
GameViewController.m
GameViewController.xib
Create your interfaces in your xib.
And now if you have to move to this class and show its view, write the following code:
-(IBAction)startButtonClicked{
GameViewController *gamePage = [[GameViewController alloc] initWithNibName:#"GameViewController" bundle:nil];
[[self navigationController] pushVIewController:gamePage animated:YES];
[gamePage release]; //If not used ARC
}

Switching Views gives me a SIGABRT Message

I'm having a little predicament switching between views, here.
Alright, so, I have this view controller class in my iPhone project called "BaseViewController," which is the default, which has a button called "GoToNextView." I added another ViewController to the storyboard called "NextViewController," and then I created another custom view controller class called "NextViewController." Under the inspector window for NextViewController on the storyboard I changed its custom class to "NextViewController;" I'm assuming everything should be hooked up, now. When I click on the "GoToNextView" button, though, the application stops with a SIGABRT message.
Here's the code for my button click action in the BaseViewController class.
- (IBAction)Transition_Next:(id)sender
{
nextViewController = [[NextViewController alloc]
initWithNibName:#"SecondView"
bundle:[NSBundle mainBundle]];
[self.view addSubview:nextViewController.view];
}
What might I be doing wrong, here?
Thanks in advance...
You can use:
- (IBAction)Transition_Next:(id)sender
nextViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:nextViewController animated:YES];
}
Instead of your code and it should work! Hope that helps!
You can use [self.navigationController pushViewController:nextViewController animated:NO]; or if you don't have a navigation controller, you may use
[self presentModalViewController:nextViewController animated:NO]; instead of [self.view addSubview:nextViewController.view];

Objective-C: Help - my xib won't show?

I have the following Objective-C code:
MainMenu *main= [[MainMenu alloc] initWithNibName:nil bundle:nil];
[[self navigationController]pushViewController:main animated:YES];
NSLog(#"hello");
I have a class called 'MainMenu' with a corresponding header file and xib file. No matter what I do, it simply won't show. I have confirmed that the code gets to the above, because of the NSLog('hello').
I've been pulling my hair out for hours now and I simply cannot get to the bottom of it.
I hope someone can help,
Edit - still having problems...
Here are some screenshots of my project setup:
Ok, so I tried this:
MainMenu *main= [[MainMenu alloc] initWithNibName:nil bundle:nil];
[[self navigationController]pushViewController:main animated:YES];
[self.view addSubview:main.view];
But it still doesn't work...
Many thanks in advance,
The fact that initWithNibName is nil should not be the problem because if it is given nil it looks for a nib with the exact name of the class.
Two things:
1) Make sure you have run a clean recently and make sure that file is correctly being loaded.
2) Make sure navigationController is not nil, if it is then you need to make sure you make a navigation controller if you are not intending on using a navigation controller, consider using:
- (void)presentModalViewController:(UIViewController *)modalViewController
animated:(BOOL)animated
Why are you setting the nibName to nil? If the name of the nib is MainMenu, then you want:
MainMenu *main= [[MainMenu alloc] initWithNibName:#"MainMenu" bundle:nil];
[self.navigationController pushViewController:main animated:YES];
[main release];
Are you sure that you have a UINavigationController in order to push a new view?
Hope that Helps!
Dont pull your hair just look at your code closely: You have nil in your initWithNibName. Whats MainMenu is it a viewController or what ? and place your correct nib for your to get Hello.
Updated as asked :
MainMenu *main= (MainMenu *)[MainMenu alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:main];
[self.navigationController presentModalViewController:nav animated:YES];
NSLog(#"hello");
I guess the issue is that you do not have a navigation controller set up .Try to present the view controller by
[self presentModalViewController:main animated:YES];
Verify that the object owner is in fact MainMenu and the view is connected.
In the MainMenu NIB, select File's Owner and the click on the Identity Inspector. Class should match your VC class name.
Then select the main View in your NIB and click on the Connections Inspector. The view outlet should be connected to your File's Owner.
If those are both set correctly, then post some more surrounding code. Notable point, if those are both set,
MainMenu * main= [[MainMenu alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:main animated:YES];
will load the correct NIB and display it.
What are you seeing? Another point if MainMenu is a subclass of some other VC with a NIB you will have to change the base class' init to override the default behavior, for example:
self = [super initWithNibName:nibNameOrNil == nil ? #"BaseViewController" : nibNameOrNil bundle:nibBundleOrNil]
But in that case you would have to specific the NIB that will override the base view controller's.
Post more code and let us know what you are seeing when you run.

iPhone creating nested views

I'm noob in iPhone programming.
I want to create navigation-based application with several nested views, when first and second are UITableView-type, and the third is simple UIView with my own interface, which I built in Interface builder.
When I run the App, touch the row on the first table-view I transfer to the second, and when I touch the row on the second, I transfer to the third, but on the third View I don't see my interface, which I built in Interface Builder, just blank screen.
This is part of my code, where I try to call my third view:
if(self.reportView == nil){
ReportTypeViewController *viewController =
[[ReportTypeViewController alloc] initWithNibName:#"ReportTypeViewController"
bundle:[NSBundle mainBundle]];
self.reportView = viewController;
[viewController release];
}
[self.navigationController pushViewController:self.reportView animated:YES];
self.reportView.title = #"Reports";
That's OK, guys.
I've just didn't add text to my button, that lies on my view, that's why it was blank.
You don't need [NSBundle mainBundle];
it should look like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ReportViewController *vc = [[ReportViewController alloc] initWithNibName:ReportViewController bundle:nil];
vc.title = #"Reports";
[self.navigationController pushViewController:vc animated:YES];
[vc release];
}
If you just create the view on demand with the selection it's much easier.
In IB, make sure you remembered to set the Class of File's Owner to ReportTypeViewController, and make sure that the view outlet of File's Owner is connected to your view.
Does ReportTypeViewController really inherit from UIViewController, or is it a UIView, as you say you built in IB?
navigationController pushViewContoller:animated: needs a UIViewController not a UIView. In IB, you can add a UIViewController and move your UIView onto it, or create it programmatically.

Removing a UIView

So I have a view controller called MainViewController with a button which when I press this code is called:
NewViewController *newViewController;
newViewController = [[NewViewController alloc] initWithNibName:#"NewView" bundle:nil];
[self.navigationController.view addSubview:newViewController.view];
[newViewController release];
This brings in the new view which works great. However, how can I remove this view from a button within it? In an application I wrote a while ago I simply created a method in MainViewController called RemoveView and within the XIB file for NewViewController I selected FirstResponder and then RemoveView for the button. This works but I can not replicate it in my new project and don't really understand how it works anyway!
It's not the remove view code I'm looking for, more the way of getting the method to call from another class.
If anyone could help me that would be great! :)
Thanks
Drawing the line in Interface Builder does the same thing as calling
[theButton addTarget:theController action:#selector(theAction) forControlEvents:UIControlEventTouchUpInside];
theAction needs to be a method that is defined with a type of IBAction.
For your situation, in your NewViewController.h, declare
- (IBAction)removeView;
Then in NewViewController.m:
- (void)removeView
{
[self.view removeFromSuperview];
}
In your newView.xib file, you should be able to drag a line from the UIButton that you've drawn to your File's Owner, and select the removeView action.