Newbie question. I created a UIView named TestView with 2 IBOutlet UIButton. I would like to add this view to initially launched view controller.
The header file of the TestView is as follow:
#import <UIKit/UIKit.h>
#interface TestView : UIView {
IBOutlet UIButton *btn1;
IBOutlet UIButton *btn2;
}
#end
I try to add the view to the screen by:
TestView *view = [[TestView alloc] init];
[self.view addSubview:view];
[view release];
but I got no response. How can I add TestView to the original view ?
p.s. TestView as a XIB too, named TestView.xib
Environment:
xCode 4
iOS 4.3.2
u have to specify the frame
frame means orgin + size
here orgin (0,0)
size (200,200)
otherwise iOs dont know where to draw this view
TestView *view = [[TestView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[view setBackgroundColor:[UIColor greenColor]];
[view addSubview:btn1];
[view addSubview:btn2];
//here add whatever u want
[self.view addSubview:view];
[view release];
You shouldn't call it view because your UIView's got already an instance variable called view.
You could also create this using Interface Builder and linking your elements to your Outlets, it will be easier
Related
I am new in ios development. I want to develop custom navigation button method.
-(void) handleNext:(id)sender
{
MRGAppDelegate *appDelegate = (MRGAppDelegate*)
[[UIApplication sharedApplication]delegate];
[appDelegate.viewController GotoDirectoryView:self.restListViewController calledView:self.view];
}
in MRGViewController.m
-(void) GoToDirectoryView:(RestaurantListViewController*) resViewContrller calledView:(UIView*)viewControllerView
{
self.resListViewController = resViewController;
[resViewController release];
[viewControllerView removeFromSuperview];
[self.resListViewController viewDidLoad];
[self.view addSubview:self.resListViewController.view];
}
But Don't call to RestaurantListViewController viewDidLoad don't call. No error appear. I don't know why? Please help me.
viewDidLoad, viewWillAppear etc are view controller life cycle method, these are called automatically when view is loaded on navigation stack. You won't need to call these method at all.
Currently you are simply adding a view on existing view controller, while you need to load a new view controller if you want view controller life cycle methods to get called.
Use like -
[self.navigationController pushViewController:restListViewController animated:YES];
[restListViewController release];
EDIT 1 -
restListViewController = [[RestaurantListViewController alloc] init];
[self presentModalViewController:restListViewController animated:YES];
[restListViewController release];
I am using model "Master-detail application" for my app and would like to set different initial view. Is it possible? I want to display UIViewController with menu buttons and one of these buttons should load the Navigation controller of the previous original initial view.
When I change the initial view in storyBoards UI to ViewController and link the button to NavigationController , the app crushes.
What would be the right thing to do?
Thank you for your time!
Include this code to the AppDelegate:
- (void)tellDelegateToFlip{
MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:#"MasterViewController" bundle:nil];
masterViewController.managedObjectContext = self.managedObjectContext;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4];
[UIView setAnimationTransition:UIViewAnimationOptionTransitionFlipFromLeft forView:self.window cache:YES];
[self.navigationController pushViewController:masterViewController animated:YES];
[UIView commitAnimations];
}
And this method to the Initial view to call the master view:
-(IBAction)flipToMaster:(id)sender{
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate tellDelegateToFlip];
}
And You're done!
I am new in IOS development .Now i implement a UINavigationController to a UIViewController, but this give a gap from the top of the view .What is the reason for this? and How can avoid this gap?
This is My code
- (void)viewDidLoad
{
[self.view setBackgroundColor:[UIColor greenColor]];
SubClass *subClass=[[SubClass alloc]initWithNibName:#"SubClass" bundle:nil];
UINavigationController *navi=[[UINavigationController alloc]initWithRootViewController:subClass];
[self.view addSubview:navi.view];
[super viewDidLoad];
}
The Following is the Output.
If anybody know please help me. Thanks in advance.
U can add one more line
[navi.view setFrame: [self.view bounds]];
- (void)viewDidLoad
{
[self.view setBackgroundColor:[UIColor greenColor]];
SubClass *subClass=[[SubClass alloc]initWithNibName:#"SubClass" bundle:nil];
UINavigationController *navi=[[UINavigationController alloc]initWithRootViewController:subClass];
[navi.view setFrame: [self.view bounds]];
[self.view addSubview:navi.view];
[super viewDidLoad];
}
On my main view controller I have a button that shows a new view.
- (IBAction)showInformation:(id)sender {
InfoViewController *controller = [[InfoViewController alloc] initWithNibName:#"InfoView" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[self.view addSubview:[controller view]];
}
On my InfoView.xib I added a UINavigationController and made the proper connections. However when I the InfoViewController loads is not showing the UINavigation as I want it to.
Currently I have this on viewDidLoad
self.view = [navController view];
And on the header file I have
IBOutlet UINavigationController *navController;
#property (nonatomic, retain) IBOutlet UINavigationController *navController;
What am I missing?
UINavigation did not show you mean UINavigationController did not appear?
1 If you use UINavigationController, you need at least two views: one for the default view that appears with the controller and another that is pushed two.
I can only see one view here, which is InfoViewController.
If you use UINavigationController, you may want to use the pushViewController method.
2 If you use presentModalViewController method, no UINavigationCOntroller is needed.
Almost all the examples I see are done with IB but I don't want to use IB.
What I want to do is, when a user selects a row in the table a UIWebView will be pushed onto the stack and load that particular page while keeping the tab bar and navigation bar. I don't want all the features of a browser rather just be able to scroll through a page beacause the rest of my app controls how a person can navigate through the website through tables.
So I've been able to push other viewcontrollers but pushing a UIWebView with the same method doesn't work.
Here is what I have so far..
This is the Threads.h file
#import "ThreadContent.h"
#import <UIKit/UIKit.h>
#interface Threads : UITableViewController {
NSMutableArray *threadName;
NSMutableArray *threadTitle;
UIActivityIndicatorView *spinner;
NSOperationQueue *operationQueue;
UILabel *loadingLabel;
NSMutableDictionary *cachedForum;
NSMutableArray *forumID;
NSInteger *indexPathRowNumber;
NSMutableArray *threadID;
ThreadContent *threadContent;
}
#property (nonatomic, assign) NSMutableArray *forumID;
#property (nonatomic, assign) NSInteger *indexPathRowNumber;
#end
Part of my Threads.m file where I am trying to push the UIWebView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"%i",indexPath.row);//get row number
NSLog(#"%#", [threadID objectAtIndex:indexPath.row]);//thread id
//forums.whirlpool.net.au/forum-replies.cfm?t=
//NSString *urlString = [NSString stringWithFormat:#"forums.whirlpool.net.au/forum-replies.cfm?t=%#", [threadID objectAtIndex:indexPath.row]];
threadContent = [[ThreadContent alloc] init];
[self.navigationController pushViewController:threadContent animated:YES];
}
My WebView files.. well i'm not sure how to do that? I did make it a "UIWebView" subclass but if i try to push it on to the stack i get a crash saying it needs it to be a "UIViewController" subclass.
UIWebView is a subclass of UIView not UIViewController.
You need to subclass UIViewController (call it for example WebViewController)
And in the viewDidLoad method create a UIWebView and add it to the view, using addSubview:
You can pass the URL to load in the webView as a property of the WebViewController
-(void)viewDidLoad {
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubView:webView];
[webView loadRequest:[NSURLRequest requestWithURL:self.urlToLoad]];
[webView release];
}
and in Threads
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"%i",indexPath.row);//get row number
NSLog(#"%#", [threadID objectAtIndex:indexPath.row]);//thread id
//forums.whirlpool.net.au/forum-replies.cfm?t=
//NSString *urlString = [NSString stringWithFormat:#"forums.whirlpool.net.au/forum-replies.cfm?t=%#", [threadID objectAtIndex:indexPath.row]];
threadContent = [[ThreadContent alloc] init];
threadContent.urlToLoad = [NSURL URLWithString:urlString];
[self.navigationController pushViewController:threadContent animated:YES];
}
(or make the webView a property and call the load method in just before or after you push the WebViewController in Threads, I'm not 100% sure if this way would work)