I have this in the App Delegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
OneViewController *vc = [[OneViewController alloc] initWithNibName:#"OneViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[self.window setRootViewController:nav];
[self.window makeKeyAndVisible];
return YES;
}
and this in OneViewController
- (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 from its nib.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
[button setTitle:#"Hi" forState:UIControlStateNormal];
[self.view addSubview:button];
}
But when I run the app in the simulator, the OneViewController screen is blank. It has a xib file which is just the default blank xib.
the button's background color and the title's font color both are white by default in iOS 7. can you set the button's background color or button's title color to black to make the button to appear?
Related
I try to add a UISegmentedControl to the UINavigationBar but when running, the UISegmentedControl is not showing.
here is a code
Appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
MainViewController2 *mainVC = [[MainViewController2 alloc] init];
self.window.rootViewController = mainVC;
return YES;
}
MainViewController2.m
#import "MainViewController2.h"
#interface MainViewController2 ()
#end
#implementation MainViewController2
#synthesize firstVC;
#synthesize segment;
- (void)viewDidLoad {
[super viewDidLoad];
[self initSegment];
}
-(void)initSegment{
segment.frame =CGRectMake(10,100,199,100);
segment = [[UISegmentedControl alloc]initWithItems:#[#"seg1", #"seg2", #"seg3"]];
self.navigationItem.titleView = segment;
[self.view addSubview:firstVC];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
You can directly add segment in navigation bar as a title view, using storyboard.
Here is programatically using your code:
Note: Don't forget to embed your view controller with navigation controller. (Either programatically or using storyboard)
- (void)viewDidLoad {
[super viewDidLoad];
[self initSegment];
}
-(void)initSegment{
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:#[#"seg1", #"seg2", #"seg3"]];
self.navigationItem.titleView = segment;
}
Add following code to your AppDelegate.m
//
// AppDelegate.m
// OBJC_Test
//
// Created by Krunal on 10/10/17.
// Copyright © 2018 Krunal. All rights reserved.
//
#import "AppDelegate.h"
#interface AppDelegate ()
#end
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
/*
UIViewController *viewController = [[UIViewController alloc] init];
viewController.view.frame = self.window.bounds;
viewController.view.backgroundColor = [UIColor whiteColor];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
*/
//or try this according to your code
MainViewController2 *mainVC = [[MainViewController2 alloc] init];
mainVC.view.frame = self.window.bounds;
mainVC.view.backgroundColor = [UIColor whiteColor];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mainVC];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
Add this code to your AppDelegate.m in the didFinishLaunchingWithOptions function to embed a navigation controller programmatically.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
MainViewController2 *mainVC = [[MainViewController2 alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mainVC];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
Below code might be useful to you
AppDelegate.h
//Declare UINavigationcontroller
#property (strong, nonatomic) UINavigationController *navigationController;
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
MainViewController2 *mainVC = [[MainViewController2 alloc] init];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:mainVC];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
In MainViewController2.m use below code
#import "MainViewController2.h"
#interface MainViewController2 ()
#end
#implementation MainViewController2
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setUpSegmentalController];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)setUpSegmentalController{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 44)];
//view.backgroundColor = [UIColor redColor];
NSArray *itemArray = [NSArray arrayWithObjects: #"One", #"Two", #"Three", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(0, 0, 250, 44);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
[segmentedControl addTarget:self action:#selector(segmentControlAction:) forControlEvents: UIControlEventValueChanged];
segmentedControl.selectedSegmentIndex = 1;
[view addSubview:segmentedControl];
self.navigationItem.titleView = view;
}
- (void)segmentControlAction:(UISegmentedControl *)segment
{
NSLog(#"selectedSegmentIndex-----%ld",segment.selectedSegmentIndex);
if(segment.selectedSegmentIndex == 0)
{
// code for the first button
}
}
Use this code.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
MainViewController2 *mainVC = [[MainViewController2 alloc] init];
self.window.rootViewController = mainVC;
[self.window makeKeyAndVisible];
return YES;
}
I have a problem to which I can't found a solution right now. In my application I have multiple buttons in my NavigationBar which are required throughout the app, rather than creating the buttons in every view controller I want to make a sub class of UINavigationBar or UINavigationController(i don't know which one). So that whenever the user moves between the views the navigation bar always contains those buttons. I have searched very much till now regarding this but couldn't found anything worth.
Please suggest me a way to do so, thanks in advance.
#import "CustomNavBar.h"
#implementation CustomNavBar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.tintColor=[UIColor greenColor];
}
return self;
}
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:#"Custom-Nav-Bar-BG.png"];
[image drawInRect:CGRectMake(0, 0, 40, self.frame.size.height)];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeContactAdd];
[btn drawRect:CGRectMake(42, 0, 40, self.frame.size.height )];
UIButton *btn2=[UIButton buttonWithType:UIButtonTypeContactAdd];
[btn2 drawRect:CGRectMake(82, 0, 40, self.frame.size.height )];
}
#end
You can subClass standard UINavigationBar to achieve this
#interface CustomNavigationBar : UINavigationBar
- (id)initWithFrame:(CGRect)frame;
#end
#implementation CustomNavigationBar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UIButton *btn=[UIButton buttonWithType:UIButtonTypeContactAdd];
[btn addTarget:self action:#selector(<#selector#>) forControlEvents:<#(UIControlEvents)#>]
[self addSubview:btn];
...
...
}
return self;
}
- (void)drawRect:(CGRect)rect {
[[UIColor redColor] setFill];
UIRectFill(rect);
}
#end
To use this is StoryBoard or xib, simply change standard class name to CustomNavigationBar.
OR
If you want it programmatically
In AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController *navVC = [[UINavigationController alloc] initWithNavigationBarClass:[CustomNavigationBar class] toolbarClass:nil];
UIStoryboard *mStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ViewController *VC1 = [mStoryboard instantiateViewControllerWithIdentifier:#"VC"];
[navVC setViewControllers:[NSArray arrayWithObject:VC1] animated:NO];
[self. window setRootViewController:navVC];
[self. window makeKeyAndVisible];
return YES;
}
I just added a new UIWebView and it doesn't seem to be showing up. I'm pretty new here, so this could be a very simple error.
I created the view using the storyboard and dragged it into my GuideViewController.h
This is my GuideViewController.h
#import <UIKit/UIKit.h>
#interface GuideViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIWebView *newsWebView;
#end
This is my GuideViewController.m
#import "GuideViewController.h"
#interface GuideViewController ()
#end
#implementation GuideViewController
#synthesize newsWebView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *newsURL = [NSURL URLWithString:#"http://www.yahoo.com"];
NSURLRequest *newsRequest = [NSURLRequest requestWithURL:newsURL];
[newsWebView loadRequest:newsRequest];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
This is what I'm using to add the view to my app delegate. I already have a sidebar menu added.
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
VidViewController *vidViewController = [[VidViewController alloc] init];
GuideViewController *newsWebView = [[GuideViewController alloc] init];
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:vidViewController];
[self.window setRootViewController:navController1];
SelectVideo1 *selectVideo1 = [[SelectVideo1 alloc] initWith:#"Test"];
//[self.navController pushViewController:selectVideo1 animated:YES];
SelectVideo1 *selectVideo2 = [[SelectVideo1 alloc] initWith:#"Latest News"];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *tabStoryBoard = [UIStoryboard storyboardWithName:#"TabStoryboard" bundle:nil];
UIStoryboard *navStoryBoard = [UIStoryboard storyboardWithName:#"NavStoryboard" bundle:nil];
UINavigationController *navController = [navStoryBoard instantiateViewControllerWithIdentifier:#"Nav Controller"];
UITabBarController *tabController = [tabStoryBoard instantiateViewControllerWithIdentifier:#"Tab Controller"];
ViewController *redVC, *greenVC;
redVC = [[ViewController alloc] init];
greenVC = [[ViewController alloc] init];
RootController *menuController = [[RootController alloc]
initWithViewControllers:#[navController, selectVideo1, selectVideo2, tabController, newsWebView]
andMenuTitles:#[#"Test", #"Videos", #"Latest News", #"Walkthroughs", #"Official News"]];
self.window.rootViewController = menuController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
#end
The view shows up in the menu bar, but when I click it, nothing happens. I believe the problem lies in my appdelegate and how I call the view?
If you're using a nib file to define your GuideViewController, use
GuideViewController *newsWebView = [[GuideViewController alloc] initWithNibName:#"Insert_Nib_Name_Here" bundle:nil];
If you define your GuideViewController in a storyboard, use
UIStoryboard *navStoryBoard = [UIStoryboard storyboardWithName:#"NavStoryboard" bundle:nil];
GuideViewController *newsWebView = [navStoryBoard instantiateViewControllerWithIdentifier:#"Insert_Guide_Controller_Name_Here"];
If that doesn't work, make sure your IBOutlet UIWebView is connected (control+drag from the UIWebView object in your nib file)
You can also just add the UIWebView programmatically.
In GuideViewController.h, you probably want to conform to the UIWebViewDelegate protocol. Implement the methods to control the UIWebView's functionality
#interface GuideViewController : UIViewController <UIWebViewDelegate>
In GuideViewController.m...
- (GuideViewController*)init
{
self = [super init];
if (self) {
//Adjust the frame accordingly if you don't want it to take up the whole view
self.newsWebView = [[UIWebView alloc] initWithFrame:self.view.frame];
self.newsWebView.delegate = self;
[self.view addSubview:self.newsWebView];
}
return self;
}
in loggerViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
UIView* mainView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.view addSubview:mainView]; // <-- Problem is here
}
loggingViewController is an iVar of my appDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
.
.
loggingViewController = [[loggerViewController alloc] init];
[loggingViewController.view setBackgroundColor:[UIColor blueColor]];
// [loggingViewController loadView];
[self.view addSubview:loggingViewController.view];
}
I was expecting my AppDelegate to call loggingViewController, which in turn, set up it's own subviews inside and it would be done. But instead the viewDidLoad gets called recursively I don't understand why?
Try it like this,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
loggingViewController = [[loggerViewController alloc] init];
self.window.rootViewController = loggingViewController;
[self.window makeKeyAndVisible];
}
The reason for recursive calling is that your self.view is nil and hence it is try to call again and again when you are trying to add it as a subview of appdelegate's view.
- (void)viewDidLoad
{
[super viewDidLoad];
UIView* mainView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.view setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:mainView];
}
In AppDelegate, I want to create a UIViewController subclass and add it's view. The viw itself will be specified in code - there is no nib.
Based on the apple docs, I should use
initWithNibName:nil bundle:nil];
and then in loadView of the controller, I add my subviews etc.
However, the follwing test code below does not work for me. I modelled the AppDelegate code on Apple's PageControl demo, simply because my app will implement a similar structure (specifically a base controller to manage a paged scroll view, and an array of other controller's to build the pages).
But I suspect my AppDelegate code is the problem, since logging proves that initWithNibName:: and loadView both fire. The app as below runs, but the screen is blank. I am expecting a green view with a label.
AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
ScrollerController *controller = [[ScrollerController alloc] initWithNibName:nil bundle:nil];
[self.window addSubview:controller.view];
[self.window makeKeyAndVisible];
return YES;
}
ScrollerController (the UIViewController subclass)
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)loadView{
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
UIView *contentView = [[UIView alloc] initWithFrame:applicationFrame];
contentView.backgroundColor = [UIColor greenColor];
self.view = contentView;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 100, 40)];
[label setText:#"Label created in ScrollerController.loadView"];
[self.view addSubview:label];
}
Try to use:
self.window.rootViewController = controller;
instead of
[self.window addSubview:controller.view];
Note, that you should also #synthesize window; and create it
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
Instead of initWithNibNamed:, just use alloc and init or any of the other designated initalizers for the view controller. Here is an example from a project
hoverViewController=[[BDHoverViewController alloc] initWithHoverStatusStyle:BDHoverViewStatusActivityProgressStyle];
self.window.rootViewController=hoverViewController;
[self.window makeKeyAndVisible];
also, the correct form( for now anyways) for adding the root view controller to the window in the app delegate is like this:
self.window.rootViewcontroller=controller;
[self.window makeKeyAndVisible];
You don't need to add the view to the window. The above code does it automatically.
Good luck,
T