I want to insert a RootViewController before my SplitViewController.
My code in AppDelegate is:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[_window addSubview:[_viewController view]];
[_window makeKeyAndVisible];
return YES;
}
Delegate call to my rootviewcontroller and in this i want to call to my splitviewcontroller:
- (void)viewDidLoad
{
[super viewDidLoad];
SplitViewController *splitViewController = [SplitViewController initApp];
[[self navigationController] pushViewController:splitViewController animated:YES];
}
In my SplitViewController i have this method:
+ (YetSplitViewController*) initApp
{
YetSplitViewController * cont = [[YetSplitViewController alloc] initWithNibName:#"YetSplitViewController" bundle:nil];
if (NO)
{
cont.splitWidth = 15.0;
}
return [cont autorelease];
}
After that not found good.......
My first code call from delegate to my SplitViewController:
#interface TemplateAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) IBOutlet UIWindow *window;
#property (readonly, nonatomic) IBOutlet YetSplitViewController *splitViewController;
#property (readonly, nonatomic) IBOutlet MasterViewController *rootViewController;
#property (readonly, nonatomic) IBOutlet DetailViewController *detailViewController;
#end
#implementation TemplateAppDelegate
#synthesize window = _window;
#synthesize splitViewController = _splitViewController;
#synthesize rootViewController = _rootViewController;
#synthesize detailViewController = _detailViewController;
- (void)dealloc
{
[_window release];
[_splitViewController release];
[_rootViewController release];
[_detailViewController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (NO)
{
_splitViewController.splitWidth = 15.0;
}
[_window addSubview:_splitViewController.view];
[_window makeKeyAndVisible];
return YES;
}
#end
Related
The idea is that I hava a custom view where the user can drag and drop one or more files and the controller is able to save the path of files into an array.
How can I run a method from AppDelegate after the user drops the file in the interface?
I have these files:
AppDelegate.h:
#import <Cocoa/Cocoa.h>
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSScrollView *table;
#property (assign) IBOutlet NSWindow *window;
#end
AppDelegate.m:
#import "AppDelegate.h"
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
}
#end
DropView.h:
#import <Cocoa/Cocoa.h>
#interface DropView : NSView <NSDraggingDestination>
#property (assign) IBOutlet NSScrollView *table;
#property NSArray *draggedFilePaths;
#end
DropView.m:
#import "DropView.h"
#implementation DropView
#synthesize draggedFilePaths;
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self registerForDraggedTypes:[NSArray arrayWithObject:NSURLPboardType]];
}
return self;
}
-(NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender{
return NSDragOperationGeneric;
}
-(NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender{
return NSDragOperationCopy;
}
-(BOOL)prepareForDragOperation:(id<NSDraggingInfo>)sender{
return YES;
}
-(BOOL)performDragOperation:(id<NSDraggingInfo>)sender{
NSPasteboard* prb;
prb= [sender draggingPasteboard];
draggedFilePaths = [prb propertyListForType:NSFilenamesPboardType];
return YES;
}
- (void)concludeDragOperation:(id<NSDraggingInfo>)sender{
[self setNeedsDisplay:YES];
NSLog(#"path %#",draggedFilePaths);
[self populateTable];
}
- (void)drawRect:(NSRect)dirtyRect
{
}
-(void)populateTable{
NSLog(#"yes");
}
#end
Import AppDelegate.h into DropView.m, and call the method you want to run from the performDragOperation: method.
-(BOOL)performDragOperation:(id<NSDraggingInfo>)sender{
NSPasteboard* prb;
prb= [sender draggingPasteboard];
draggedFilePaths = [prb propertyListForType:NSFilenamesPboardType];
[(AppDelegate *)[[NSApplication sharedApplication]delegate] doWhatever:draggedFilePaths];
return YES;
}
Where doWhatever: is a method implemented in the app delegate.
Im trying to send some info from a modal view to a delgate. But it seems like my delegate doesnt follow through, it gets null. It looks like this in IB http://i.imgur.com/7oaxb.png.
But it works if i remove the navigationController that is right before the modal view and use the buttons in the View.
please help, ive tried for like 5 hours... :/
Heres the modalViewController code:
#import
#import "Link.h"
#protocol modalViewDelegate <NSObject>
-(void)closeview;
-(void)saveLink:(Link *)link;
#end
#interface modelViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *titel;
#property (weak, nonatomic) IBOutlet UITextField *url;
#property (nonatomic, weak) id <modalViewDelegate> delegate;
- (IBAction)exitModal:(id)sender;
- (IBAction)saveLink:(id)sender;
#end
and .m:
#import "modelViewController.h"
#interface modelViewController ()
#end
#implementation modelViewController
#synthesize titel;
#synthesize url, delegate;
- (IBAction)exitModal:(id)sender {
//[self.delegate closeview];
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)saveLink:(id)sender {
if (titel.text.length > 0 && url.text.length > 0) {
NSString *urlen = [NSString stringWithFormat:#"%#", url.text];
Link *linken = [[Link alloc] initWithURL:[NSURL URLWithString:urlen]];
linken.title = titel.text;
NSLog(#"%#", delegate); **//returns null when pressing button** it returns null if i put it in viewDidLoad to..
[self.delegate saveLink:linken];
[self dismissModalViewControllerAnimated:YES];
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:#"warning" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil, nil];
[alertView show];
}
}
#end
the MasterViewController .h (that pushes the modalview:
#import <UIKit/UIKit.h>
#import "modelViewController.h"
#class DetailViewController;
#interface MasterViewController : UITableViewController <modalViewDelegate>
....
and .m
#import "MasterViewController.h"
#import "DetailViewController.h"
#implementation MasterViewController
#synthesize detailViewController = _detailViewController;
#synthesize links;
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"linkPush"]) {
// Skicka länken till detaljvyn
DetailViewController *detailVC = segue.destinationViewController;
detailVC.link = [self.links objectAtIndex:self.tableView.indexPathForSelectedRow.row];
NSLog(#"%#", detailVC);
}
//this is the modalview "pusher"
if ([segue.identifier isEqualToString:#"newLink"]) {
modelViewController *mvc = segue.destinationViewController;
mvc.delegate = self;
NSLog(#"%#", mvc.delegate);
}
}
- (void)closeview {
[self dismissModalViewControllerAnimated:YES];
//[self.tabBarController dismissModalViewControllerAnimated:YES];
}
-(void)saveLink:(Link *)link{
NSLog(#"hello");
[links insertObject:link atIndex:links.count]; //updates a Tableview and works fine if delegate is called
[self.tableView reloadData];
//[self dismissModalViewControllerAnimated:YES];
}
If your destination view controller is wrapped into a navigation controller, you have to refer to it differently in prepareForSegue:
UINavigationController *nav = segue.destinationViewController;
DetailViewController *dvc = [nav.viewControllers objectAtIndex:0];
Now setting the properties, including the delegate, should work.
I'm adding a view controller's view to my window in application:didFinishLaunchingWithOptions: but the view does not become visible. I'm not sure what's wrong.
Here's my app delegate code:
#class ToolBar;
#class MainViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UIWindow *Window;
//UIToolbar *toolbar;
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) MainViewController *mainViewController;
#property (strong, nonatomic) ToolBar *toolbar;
#end
#import "AppDelegate.h"
#import "MainViewController.h"
#import "ToolBar.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize mainViewController = _mainViewController;
#synthesize toolbar =toolbar;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
self.mainViewController = [[[MainViewController alloc]init]autorelease];
[self.window addSubview:self.mainViewController.view];
//[self.window makeKeyAndVisible];
self.toolbar = [[[ToolBar alloc]init]autorelease];
[self.window addSubview:toolbar.view];
[self.window makeKeyAndVisible];
return YES;
}
Well, to start with you're not setting the frame for your toolbar.
But why are you adding a toolbar to your app's main window? I would expect it to be a subview of your mainViewController's view.
so i'm testing this so i can use it on my bigger project.
I have A tabbarcontroller named TabBar
this TabBar has 2 tabs every tab has a navigationcontroller. A viewController with a button (OkButtonViewController) when you click this button you go to the viewcontroller with the label (LabelViewController). The OkButton View Controller is always in portrait and the labelViewController can switch orientation. this works only in one situation it goes wrong. when you are in the LabelViewController, orientated in landscape, and you switch tabs the OkButtonViewController is also in landscape, and stays in landscape. How can i force the viewcontroll to go back to portrait?
here is my code.
I probably need to add something in the TabBar or in the RotatingTabBarAppDelegate. I just don't know what.
TabBar.m
#import "TabBar.h"
#implementation TabBar
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
#end
RotatingTabBarAppDelegate.h
#import <Foundation/Foundation.h>
#import "TabBar.h"
#class RotatingTabBarAppViewController;
#interface RotatingTabBarAppDelegate : NSObject<UIApplicationDelegate>
{
IBOutlet UIWindow *window;
}
#property (nonatomic, strong) UIWindow *window;
#end
RotatingTabBarAppDelegate.m
#implementation RotatingTabBarAppDelegate
#synthesize window;
-(void) applicationDidFinishLaunching:(UIApplication *)application
{
UIViewController *tab1 = [[UIViewController alloc] init];
tab1.tabBarItem =[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemTopRated tag:0];
UIViewController *tab2 = [[UIViewController alloc] init];
tab2.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:1];
TabBar *tbc = [[TabBar alloc] init];
[tbc setViewControllers:[NSArray arrayWithObjects:tab1, tab2, nil]];
[window addSubview:tbc.view];
[window makeKeyAndVisible];
}
#end
OkButtonViewController.h
#import <UIKit/UIKit.h>
#interface OkButtonViewContoller : UIViewController
- (IBAction)ok;
#end
OkButtonViewController.m
#import "OkButtonViewController.h"
#import "LabelViewController.h"
#define kDetailSegue #"Detail"
#implementation OkButtonViewContoller
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)ok
{
[self performSegueWithIdentifier:kDetailSegue sender:#"test"];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:kDetailSegue]) {
((LabelViewController *)segue.destinationViewController).testTekst = sender;
}
}
#end
LabelViewController.h
#import <UIKit/UIKit.h>
#interface LabelViewController : UIViewController
#property (nonatomic, strong) NSString *testTekst;
#property (nonatomic, strong) IBOutlet UILabel *testLabel;
#end
LabelViewController.h
#import "LabelViewController.h"
#implementation LabelViewController
#synthesize testTekst;
#synthesize testLabel;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.testLabel.text = testTekst;
}
#end
sorry for the bad english.
You cannot force a view to rotate, probably Apple would reject your app, you need to find another way to design this.
I am following the big nerd ranch guide book and have modified my app delegate.h to look like this:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#interface WhereamiAppDelegate : NSObject <UIApplicationDelegate,CLLocationManagerDelegate, MKMapViewDelegate>
{
IBOutlet UITextField *locationTitleField;
IBOutlet UIActivityIndicatorView *activityIndicator;
IBOutlet MKMapView *worldView;
CLLocationManager *locationManager;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
#property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
#end
The .m looks like this:
#import "WhereamiAppDelegate.h"
#implementation WhereamiAppDelegate
#synthesize window = _window;
#synthesize managedObjectContext = __managedObjectContext;
#synthesize managedObjectModel = __managedObjectModel;
#synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
CLLocationCoordinate2D loc = [userLocation coordinate];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
[worldView setRegion:region animated:YES];
NSLog(#"didUpdateUserLocation is called");
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Create location manager object
locationManager = [[CLLocationManager alloc] init];
// There will be a warning from this line of code; ignore it for now
[locationManager setDelegate:self];
// We want all results from the location manager
[locationManager setDistanceFilter:kCLDistanceFilterNone];
// And we want it to be as accurate as possible
// regardless of how much time/power it takes
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
// Tell our manager to start looking for its location immediately
// [locationManager startUpdatingLocation];
[worldView setShowsUserLocation:YES];
// This line may say self.window, don't worry about that
[self.window makeKeyAndVisible];
return YES;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"%#", newLocation);
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"Could not find location: %#", error);
}
When I open the app it should ZOOM into my location. But it doesn't zoom, so i put an NSLog in the didUpdateUserLocation to see if it was called. But it was never printed, so it wasn't called. How do I fix this?
Same thing happened to me...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
[worldView setDelegate:self];
...
}
Thks