I have a Tabbar iOS application with three tabs. I have each tab set up to be a UIWebView. I want each web view to display a different PDF. How do I assign, through code, each webview to its corresponding pdf?
It will look something like this in your AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
WebViewController *webViewController1 = [[WebViewController alloc] initWithPDFAtURL:#"urlToPDF1"];
WebViewController *webViewController2 = [[WebViewController alloc] initWithPDFAtURL:#"urlToPDF2"];
WebViewController *webViewController3 = [[WebViewController alloc] initWithPDFAtURL:#"urlToPDF3"];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:[NSArray arrayWithObjects:webViewController1, webViewController2, webViewController3, nil]];
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
You will need to have a view controller that holds the UIWebView, and could look something like:
// WebViewController.h
#interface WebViewController : UIViewController
#end
// WebViewController.m
#interface WebViewController ()
#property (nonatomic, strong) NSURL *_pdfURL;
#end
#implementation WebViewController
#synthesize _pdfURL;
- (id)initWithPDFAtURL:(NSURL *)url {
self = [super init];
if (self) {
_pdfURL = url;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Load the PDF into a UIWebView
}
Related
I have set my self.navigationItem.title = #"Hello"; in ViewController.m but it's not showing:
AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
#interface AppDelegate ()
#end
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
ViewController *viewController = [[ViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
#pragma mark - UISceneSession lifecycle
- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options API_AVAILABLE(ios(13.0)) {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return [[UISceneConfiguration alloc] initWithName:#"Default Configuration" sessionRole:connectingSceneSession.role];
}
- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions API_AVAILABLE(ios(13.0)) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
#end
ViewController.m
#import "ViewController.h"
#import <UIKit/UIKit.h>
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = #"Hello";
self.view.backgroundColor = [UIColor systemBlueColor];
}
#end
Thanks in advance!
I'm considering that you're not using storyboard.
In your case, you have to do everything programmatically, including allocating the navbar.
- (void)viewDidLoad {
[super viewDidLoad];
UINavigationBar *navbar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];
UINavigationItem *title = [[UINavigationItem alloc] initWithTitle:#"Hello"];
self.view.backgroundColor = [UIColor systemBlueColor];
[navbar setItems:#[title]];
[self.view addSubview:navbar];
}
If you were using storyboard, this simple command would be working, without needing anything in the appDelegate.
self.navigationItem.title = #"Hello";
It is enough to say: viewController.title = #“Hello”;
https://developer.apple.com/documentation/uikit/uiviewcontroller/1621364-title
This question already has answers here:
calling methods across view controllers
(2 answers)
Closed 9 years ago.
Please give the suggestion of these code
appdelegate.h
#import <UIKit/UIKit.h>
#import "MainViewController.h"
#interface MainAppDelegate : UIResponder <UIApplicationDelegate>
{
UINavigationController *navigationController;
}
#property (strong, nonatomic) UIWindow *window;
#property (nonatomic, retain) UINavigationController *navigationController;
#property (nonatomic, retain) MainViewController *MainviewController;
appdelegate.m
#import "MainAppDelegate.h"
#import "MainViewController.h"
#implementation MainAppDelegate
#synthesize navigationController, MainviewController;
#synthesize matri;
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
matri=#"";
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.MainviewController = [[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] init];
self.window.rootViewController = self.navigationController;
[self.navigationController pushViewController:self.MainviewController animated:YES];
[self.navigationController setNavigationBarHidden:YES animated:NO];
[self.window makeKeyAndVisible];
return YES;
} //plaese provide detail of code
mainviewcontroller.h
#import <UIKit/UIKit.h>
#import "SecViewController.h"
#interface MainViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
mainviewcontroller.m
#import "MainViewController.h"
#import "MainAppDelegate.h"
#interface MainViewController ()
#end
#implementation MainViewController
{
NSArray *tableData;
MainAppDelegate *appDelegate;
}
//#synthesize pun;
- (void)viewDidLoad
{
[super viewDidLoad];
//appDelegate=(MainAppDelegate*)[[UIApplication sharedApplication]delegate ];
tableData = [NSArray arrayWithObjects:#"1",#"2",#"3",#"4", nil];
NSLog(#"tabledata : %#", tableData);
// Do any additional setup after loading the view, typically from a nib.
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"tabledata : %#", tableData);
return [tableData count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
// NSLog(#"Value at index:%#",cell.textLabel.text);
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
SecViewController *SecView = [[SecViewController alloc] initWithNibName:#"SecViewController" bundle:nil];
SecView.newarray =[tableData objectAtIndex:indexPath.row];
[self.navigationController pushViewController:SecView animated:YES ];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
i dont understand how to call secondview from first view help me out
the hole code file are for creating simple table view and to call secviewcontroller
can anyone help me to provide code how to call secview object and how to use in mainview controller
and i also created nsmutablearray with newarray object and synthesize it
can any one understand this code and give the brief description to that code and suggest me to what i miss sothat i can check this and call secviewcontroller from mainviewcontroller.
use this code into app delegate.m file
YourviewController *loginVc = [[YourviewController alloc] initWithNibName:#"YourviewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:loginVc];
self.window.rootViewController = self.navigationController;
self.navigationController.navigationBarHidden =YES;
[self.window makeKeyAndVisible];
CHeck this:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
matri=#"";
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.MainviewController = [[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.MainviewController]; //Embedding your navigationController in mainviewcontroller
self.window.rootViewController = self.navigationController;
[self.navigationController setNavigationBarHidden:YES animated:NO];
[self.window makeKeyAndVisible];
return YES;
}
And then in your mainViewCOntroller.m
#import"appdelegate.h"
And then in
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
SecViewController *SecView = [[SecViewController alloc] initWithNibName:#"SecViewController" bundle:nil];
SecView.newarray =[tableData objectAtIndex:indexPath.row];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate.navigationController pushViewController:SecView animated:YES ];
}
Do this one.
self.MainviewController = [[MainviewController alloc] initWithNibName:#"MainviewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.MainviewController];
self.window.rootViewController = self.navigationController;
self.navigationController.navigationBarHidden =YES;
[self.window makeKeyAndVisible];
I've followed several guides on passing data between view controllers, but they seem to involve setting the secondViewController's property in any equivalent of a -(void)goToNextView method. I'm trying to figure out how to assign a value to a property in a 2nd view controller from the first while using a tab bar controller created in the app delegate.
Current setup:
AppDelegate.m
#synthesize window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
...
UITabBarController *tbc = [[UITabBarController alloc] init];
FirstViewController *vc1 = [[FirstViewController alloc] init];
SecondViewController *vc2 = [[SecondViewController alloc] init];
[tbc setViewControllers: [NSArray arrayWithObjects: vc1, vc2, nil]];
[[self window] setRootViewController:tbc];
[self.window makeKeyAndVisible];
return YES;
}
...
#end
FirstViewController.h
#import "SecondViewController.h"
#interface FirstViewController : UIViewController
{
SecondViewController *vc2;
IBOutlet UISlider *sizeSlider;
}
#property (nonatomic, retain) SecondViewController *vc2;
#property (strong, nonatomic) UISlider *mySlider;
-(IBAction) mySliderAction:(id)sender;
#end
FirstViewController.m
#import "FirstViewController.h"
#import "SecondViewController.h"
#implementation FirstViewController
#synthesize vc2, mySlider;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
UITabBarItem *tbi = [self tabBarItem];
[tbi setTitle:#"xib"];
}
return self;
}
...
- (IBAction) mySliderAction:(id)sender
{
NSString *str = [[NSString alloc] initWithFormat:#"%3.2f", mySlider.value];
if(self.vc2 == nil)
{
SecondViewController *viewTwo = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
self.vc2 = viewTwo;
}
vc2.sliderValueString = str;
}
...
#end
SecondViewController.h
#import <UIKit/UIKit.h>
#import "myView.h"
#interface SecondViewController : UIViewController
{
NSString *sliderValueString;
}
#property (copy) NSString *sliderValueString;
#end
SecondViewController.m
#import "SecondViewController.h"
#implementation SecondViewController
#synthesize sliderValueString;
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle
{
self = [super initWithNibName:nil bundle:nil];
if (self) {
UITabBarItem *tbi = [self tabBarItem];
[tbi setTitle:#"View 2"];
}
return self;
}
-(void) loadView
{
CGRect frame = [[UIScreen mainScreen] bounds];
myView *view = [[myView alloc] initWithFrame:frame];
printf("Slider Value: %s", [sliderValueString UTF8String]);
[self setView: view];
}
...
#end
myView.h and .m are likely irrelevant to the question, but I've got the .h subclassing UIView, and the .m creating the view with -(id)initWithFrame:(CGRect)frame
I'm guessing that I'm assigning the 2nd VC's property in the wrong place (mySliderAction), given that the printf() in the 2nd VC is blank, so my question is: Where should the property be assigned, or what am I doing wrong that is preventing the 2nd VC from allowing it's sliderValueString to not be (or remain) assigned?
Thanks!
You are fine setting the property in mySliderAction, the trouble is that you are setting it on the wrong object.
In the App Delegate you create a viewController for tabControllers' item 2:
SecondViewController *vc2 = [[SecondViewController alloc] init];
In vc1's mySliderAction you also create an instance of vc2:
SecondViewController *viewTwo = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
This is not the instance that you navigate to via the second tab on the tabController, but it is the one who's sliderValueString property you are setting.
Instead of making a new instance here, you need to refer to the already-existing instance:
SecondViewController *viewTwo = [self.tabBarController.viewControllers objectAtIndex:1]
Then you should be ok.
I find many question to force UIViewController to landscape as default:
How to force a UIViewController to Portrait orientation in iOS 6
And try this:
- (BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
But when I use UIViewController inside UINavigationController, I can't force to landscape.Please help!
*Work NOT ok
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[navControl setNavigationBarHidden:YES];
self.window.rootViewController = navControl;
[self.window makeKeyAndVisible];
return YES;
}
*Work OK:
self.window.rootViewController = self.viewController;
Best way to do this is the create extend UINavigationController and write your orientation function inside the extended class.
Class Declaration:
#interface MyNavigationControllerViewController : UINavigationController
#property(nonatomic, assign) UIInterfaceOrientation orientation;
#property(nonatomic, assign) NSUInteger supportedInterfaceOrientatoin;
#end
Implementation of MyNavigationController
#implementation MyNavigationController
#synthesize supportedInterfaceOrientatoin = _supportedInterfaceOrientatoin;
#synthesize orientation = _orientation;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
_supportedInterfaceOrientatoin = UIInterfaceOrientationMaskLandscape;
_orientation = UIInterfaceOrientationLandscapeLeft;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return _supportedInterfaceOrientatoin;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return self.orientation;
}
#end
and use your extended like MyNavigationController as navigation controller to your rootviewcontroller.
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController *viewController;
#property (strong, nonatomic) MyNavigationControllerViewController *myNavController;
- (void) reloadAppDelegateRootViewControllerLandscape;
- (void) reloadAppDelegateRootViewController;
#end
So your application delegate didfinishlounchingwithoptions code will be as follow.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.myNavController = [[MyNavigationController alloc] initWithRootViewController:self.viewController];
[self.myNavController setNavigationBarHidden:YES];
self.window.rootViewController = self.myNavController;
[self.window makeKeyAndVisible];
return YES;
}
Only way you can provide different orientation for two different view with same navigation controller isto reload the navigation controller itself. So if you add two methods
- (void) reloadAppDelegateRootViewController{
[[[UIApplication sharedApplication].delegate window] setRootViewController:nil];
[(MyNavigationControllerViewController *)self.myNavController setOrientation:UIInterfaceOrientationPortrait];
[(MyNavigationControllerViewController *)self.myNavController setSupportedInterfaceOrientatoin:UIInterfaceOrientationMaskAll];
[[[UIApplication sharedApplication].delegate window] setRootViewController:self.myNavController];
}
- (void) reloadAppDelegateRootViewControllerLandscape{
[[[UIApplication sharedApplication].delegate window] setRootViewController:nil];
[(MyNavigationControllerViewController *)self.myNavController setOrientation:UIInterfaceOrientationLandscapeLeft];
[(MyNavigationControllerViewController *)self.myNavController setSupportedInterfaceOrientatoin:UIInterfaceOrientationMaskLandscape];
[[[UIApplication sharedApplication].delegate window] setRootViewController:self.myNavController];
}
and call these function after pushing and pop views.
Note:- I don't know whether it is a good way or bad way.
You need to go to your plist file and set the orientations. You can also do this on your project file. The plist orientations settings will override all though.
In your plist you can set both orientation options to landscape button left and landscape button right.
My app was launching fine earlier today and now I'm getting this error
"Applications are expected to have a root view controller at the end of application launch"
I've looked at other threads saying to change my code, but I never changed any code to get to this point.
Delegate.h
#interface halo4AppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate>{
UIWindow *window;
UITabBarController *tabBarController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#end
Delegate.m
#implementation halo4AppDelegate
#synthesize window;
#synthesize tabBarController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
sleep(3);
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
#pragma mark -
#pragma mark Memory management
- (void)dealloc {
[tabBarController release];
[window release];
[super dealloc];
}
#end
The xib for my FirstViewController is titles FirstView.xib , ext
This is not an error, more like a warning.
In your application delegate there is a method named application:didFinishLaunchingWithOptions: in this method you have to make this line before the end of the method self.window.rootViewController = [Some UIViewController]
again, this is not an error, you can ignore the rootViewController IF you have another way to create this rootViewController.
EDIT
This is what your method should looks like:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[viewController1, viewController2];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}