TTNavigator not opening URLs when clicking on links in TTStyledTextLabels - three20

Trying to make a simple link clicking activity work. I think I understand TTNavigator and TTStyledLabel, but can't get it to work.
Code:
#interface SomeVc : UIViewController <TTNavigatorDelegate> {
IBOutlet TTStyledTextLabel *styledTextLabel;
}
#end
#implementation SomeVc
- (void)viewDidLoad {
[super viewDidLoad];
navigator = [TTNavigator navigator];
navigator.persistenceMode = TTNavigatorPersistenceModeNone;
navigator.delegate = self;
TTURLMap* map = navigator.URLMap;
[map from:#"*" toViewController:[TTWebController class]];
styledTextLabel.text = [TTStyledText textWithURLs:someText];
[navigator openURLAction:[TTURLAction actionWithURLPath:#"http://www.cnn.com/"]];
}
- (BOOL)navigator: (TTNavigator*)navigator shouldOpenURL: (NSURL*)URL {
NSLog(#"trying to open %#", [URL absoluteString]);
return NO;
}
#end
I.e inside a viewcontroller, get the navigator and set self to be its delegate. When a link is opened, the shouldOpenURL delgate method gets called, where I will handle the URL opening myself. (I plan to let navigator handle more of it, but want to get this simple case working first.)
I have a test call at the end of viewDidLoad: which fires the delegate method fine.
Problem: I see the styledTextLabels rendered fine with URL-s, but when I tap on those, nothing happens. They don't reach the TTNavigator for some reason and I can't understand why. Feels like I'm missing some simple connection/scaffolding somewhere, but can't figure it out.
How to make it so that the links tapped in the styledtextlabel will reach the navigator delegate? Or how else should I implement this simple case with styledtextlabel? (just want to get callbacks for url taps.)

try setting the window property :
TTNavigator* navigator = [TTNavigator navigator];
navigator.window = window;
If you don't have one you can add one
navigator.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]
You might also need:
[navigator.window makeKeyAndVisible];

Related

The new UISplitViewController in iOS8 using objective c without storyboard

I try to implement adaptive UI in my app. By making UISplitViewController as the rootview controller, I can run the iPhone's code in iPad too.
I red Apple's documentation about UISplitViewController and some samples. All are using storyboards and the sample codes are available in swift only. I can not find a working version of code. So I started the code myself.
See my splitview controller class (BaseSplitViewController)
BaseSplitViewController.h:
#import <UIKit/UIKit.h>
#interface BaseSplitViewController : UISplitViewController <UISplitViewControllerDelegate>
#end
BaseSplitViewController.m:
#import "BaseSplitViewController.h"
#import "TabBarViewController.h"
#interface BaseSplitViewController ()
#property(nonatomic, strong) TabBarViewController *primaryTabBarVC;
#property(nonatomic, strong) UINavigationController *primaryNavigationController;
#property(nonatomic, strong) UINavigationController *secondaryNavigationController;
#end
#implementation BaseSplitViewController
- (instancetype)init
{
self = [super init];
if (self)
{
[self setViewControllers:#[self.primaryNavigationController, self.secondaryNavigationController]];
self.delegate = self;
self.preferredDisplayMode = UISplitViewControllerDisplayModeAutomatic;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(cellTapped:) name:#"cellTapped" object:nil];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self assignPrimaryViewController];
}
- (void)assignPrimaryViewController
{
// Need to assign tab bar controller as primary view controller here
}
- (void)assignSecondaryViewController:(UIViewController *)vc
{
// Need to update the secondary controller each time the primary controller was tapped
}
- (UINavigationController *)primaryNavigationController
{
if (!_primaryNavigationController)
{
_primaryNavigationController = [[UINavigationController alloc] init];
}
return _primaryNavigationController;
}
- (UINavigationController *)secondaryNavigationController
{
if (!_secondaryNavigationController)
{
_secondaryNavigationController = [[UINavigationController alloc] init];
}
return _secondaryNavigationController;
}
- (UITabBarController *)primaryTabBarVC
{
if (!_primaryTabBarVC)
{
_primaryTabBarVC = [[TabBarViewController alloc] init];
}
return _primaryTabBarVC;
}
#end
Some points:
The above class "BaseSplitViewController" is the rootview controller of my app.
That is, self.window.rootViewController = [[BaseSplitViewController alloc] init];
From Apple's Documentation,
"When designing your split view interface, it is best to install
primary and secondary view controllers that do not change. A common
technique is to install navigation controllers in both positions and
then push and pop new content as needed. Having these types of anchor
view controllers makes it easier to focus on your content and let the
split view controller apply its default behavior to the overall
interface."
So, I created two navigation controllers (primary/secondary) and set them as split view controllers's primary & secondary views. setViewControllers: can be used for this.
My primary view here is, tab bar view. So, inside the assignPrimaryViewController: method, I should assign my TabBarViewController as split view controller's primary view.
Here, I found two ways.
1. [self.primaryNavigationController showViewController:self.primaryTabBarVC sender:nil];
2. [self.primaryNavigationController pushViewController:self.primaryTabBarVC animated:YES];
Here, I tried with [self showViewController:self.primaryTabBarVC sender:nil]; but my tab bar view was never shown. From my understanding, here "self" means the UISplitViewController. Calling showViewController: here makes the confusion to choose the navigation controller. Because we have two navigation controllers. So we need to clearly tell that navigation controller which needs to hold the primary controller.
Primary view controller part is over. Now the real problem starts. Consider my primary view controller is the tab bar which have tableview's in it. If I tap on the cell, I need to update the secondary view's content. This is the case in Regular mode. In compact mode, I expect when the user taps on the cell, it should push the detail view (secondary view) with back button.
I expect to put the below code within assignSecondaryViewController: vc: method
[self.secondaryNavigationController pushViewController:vc animated:NO];
[self.primaryNavigationController showDetailViewController:self.secondaryNavigationController sender:nil];
But it does not works.
Questions:
What should be placed inside assignPrimaryViewController & assignSecondaryViewController: methods to get my expected result?
And I really, yes really don't know how to implement UISplitViewController's following delegate methods.
primaryViewControllerForCollapsingSplitViewController:
splitViewController:collapseSecondaryViewController:ontoPrimaryViewController:
primaryViewControllerForExpandingSplitViewController:
splitViewController:separateSecondaryViewControllerFromPrimaryViewController:
Would be really helpful, if someone explains this new UISplitViewController's behavior.
Thanks

objective-C dragging images into NSTextView subclass

I’ve created a Cocoa-made text editing application that mimicks TextEdit with some
other features added. I use a subclass of NSTextView. My problem is, I can’t drag images (jpeg for example), into my text window, as I would do in TextEdit.
I tried the following : The -init method of my subclass is as follows :
- (id)init
{
self = [super init];
if (self) {
// Add your subclass-specific initialization here.
// If an error occurs here, send a [self release] message and return nil.
[self setImportsGraphics:YES];
}
return self;
}
But the problem stays the same ... Any help appreciated.
Are you sure that that init function is being called?
Perhaps you should override and add the "setImportGraphics" line to the initWithFrame:(NSRect)frame function instead.

Where do I put the code for changing the nav bar color in xcode 4.3

Ok, it's 3 am atm and I haven't a clue where I put this:
[navigationController.navigationBar setTintColor:[UIColor redColor];
If you would please post the whole .m/.h files that would be great. Also, do I connect anything using segues or outlets? And when you create the .h/.m files do I need UINavigationController or similar selected or just the normal UIViewController? Thanks.
Update: Nevermind I got it, thanks though. Below is the code for others having my issue.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UINavigationBar *bar = [self.navigationController navigationBar];
[bar setTintColor:[UIColor lightGrayColor]];
}
Basically just add on to what's already there.
I feel stupid lol.
You can set that value right after you init your UINavigationController, i.e:
UINavigationController *controller = [[UINabvigationController alloc] initWithRoot...
[controller.navigationBar setTintColor:[UIColor redColor]];

How to rotate app with TabBar?

Hi I have a splitview app that is working fine until I add a TabBar in the rootview section. The problem is that when I add the TabBar to the rootview the app does not rotate to landscape, if I change the orientation the view remains in portrait mode.
How can I solve this?. Hope you can help
#import "SplitViewTest3AppDelegate.h"
#import "SISACWelcomeViewController.h"
#implementation SplitViewTest3AppDelegate
#synthesize window, masterViewController, splitViewController,masterViewTabBarController, searchViewController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
masterViewController = [[MasterViewController alloc] initWithStyle:UITableViewStyleGrouped];
UINavigationController *masterNavigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
masterNavigationController.tabBarItem.image = [UIImage imageNamed:#"Folder.png"];
//NewsFeedsNavigationController *newsFeedsNavigationController = [[NewsFeedsNavigationController alloc] init];
SISACWelcomeViewController *sisacWelcomeViewController = [[SISACWelcomeViewController alloc] init];
UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:sisacWelcomeViewController];
searchViewController = [[UIViewController alloc] initWithNibName:#"SearchView" bundle:nil];
searchViewController.tabBarItem.image = [UIImage imageNamed:#"Search-icon.png"];
masterViewTabBarController = [[UITabBarController alloc] init];
masterViewTabBarController.viewControllers = [NSArray arrayWithObjects:masterNavigationController, searchViewController, nil];
masterViewController.detailNavigationController = detailNavigationController;
splitViewController = [[UISplitViewController alloc] init];
splitViewController.viewControllers = [NSArray arrayWithObjects:masterViewTabBarController, detailNavigationController, nil];
splitViewController.delegate = sisacWelcomeViewController;
// Add the split view controller's view to the window and display.
[window addSubview:splitViewController.view];
//[masterNavigationController.view addSubview:tab.view];
[window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
/*
Restart any tasks that were paused (or not yet started) while the application was inactive.
*/
}
- (void)applicationWillTerminate:(UIApplication *)application {
/*
Called when the application is about to terminate.
*/
}
#pragma mark -
#pragma mark Memory management
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
/*
Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
*/
}
- (void)dealloc {
[window release];
//[tab release];
[super dealloc];
}
#end
The answer below is correct. If you are adding tabs which include using the CoreDataTableView controller (that is used with the CS193P course), be sure to add a method to allow for any orientation. If not, your split view will not work correctly.
SOLVED:
I had the same issue.
Without the TabBar all is well, add the TabBar and the rotation breaks.
I guessed that there is something broken in the responder chain or view hierarchy.
So I was about to submit as a bug. So wrote a test app to demo to Apple (because they ALWAYS ask for one), and it worked. Hooray, but why?
These are my findings from the Apple docs.
From the View Programming Guide for iOS.
Split View Controller
"A split view controller must always be the root of any interface you create."
Thus they should not be embedded within a TabBar View, although I understand that there is a workaround out in the wild.
Also:
Creating a Tab Bar Interface
"Install it as one of the two root views in a split view interface. (iPad only)"
Solution:
After much more investigation, and some trial and error, I found the issue.
Of course it seems so obvious NOW.
When the SplitView tests for shouldAutorotateToInterfaceOrientation, it tests every possible view on the whole hierarchy, that is EVERY view in the MasterView, thus EVERY view in the TabBar, and EVERY view in the DetailView, thus EVERY view in the current NavigationStack.
The fly in the ointment is that a newly created ViewController does not support Landscape by default.
Where I had gone wrong was: I had created ALL of the TabBar subviews, but not written any more code yet, because I wanted to get the SplitView with TabBar working first, thus 1 of my Tab Views had not been changed from the default.

Cocoa QuickLook initiated by NSTableView Cell

I have an NSTableView that contains 2 different Columns - one is an NSImageCell that shows a file icon, and the second is a custom subclass of NSTextFieldCell that contains a quick look button on the right of the text. When I click the Quick Look button, the following code is invoked:
[[QLPreviewPanel sharedPreviewPanel] makeKeyAndOrderFront:nil];
This does it's job and shows the blank Quick Look panel saying "No Items Selected." After I did a bit of research on the internet, I implemented a custom NSTableView subclass to be the Delegate and Data Source for the Quick Look panel. I get the notification that Quick Look asks if I want to be the delegate, and I respond with return YES. Even though I implement all methods in both QLPreviewPanelDataSource and QLPreviewPanelDelegate, at runtime I get this error on the console:
2010-12-24 15:32:17.235 BackMeUp[4763:80f] clicked: ~/Desktop/HUDTape.mov
2010-12-24 15:32:17.489 BackMeUp[4763:80f] [QL] QLError(): -[QLPreviewPanel setDelegate:] called while the panel has no controller - Fix this or this will raise soon.
See comments in QLPreviewPanel.h for -acceptsPreviewPanelControl:/-beginPreviewPanelControl:/-endPreviewPanelControl:.
2010-12-24 15:32:17.490 BackMeUp[4763:80f] [QL] QLError(): -[QLPreviewPanel setDataSource:] called while the panel has no controller - Fix this or this will raise soon.
See comments in QLPreviewPanel.h for -acceptsPreviewPanelControl:/-beginPreviewPanelControl:/-endPreviewPanelControl:.
2010-12-24 15:32:17.491 BackMeUp[4763:80f] We can now receive QL Events.
2010-12-24 15:32:18.291 BackMeUp[4763:80f] -[NSPathStore2 stringValue]: unrecognized selector sent to instance 0x5ecb10
2010-12-24 15:32:18.292 BackMeUp[4763:80f] -[NSPathStore2 stringValue]: unrecognized selector sent to instance 0x5ecb10
And the Quick Look panel does not show up, which I find rather odd. The first line above is just that I know the cell has been clicked. Anyways, here is the .m file of the custom table view subclass:
//
// BackupListTableView.m
// BackMeUp
//
// Created by Tristan Seifert on 12/24/10.
// Copyright 2010 24/7 Server. All rights reserved.
//
#import "BackupListTableView.h"
#implementation BackupListTableView
- (void) awakeFromNib {
}
// Quick Look Delegates
- (BOOL)acceptsPreviewPanelControl:(QLPreviewPanel *)panel;
{
[QLPreviewPanel sharedPreviewPanel].delegate = self;
[QLPreviewPanel sharedPreviewPanel].dataSource = self;
NSLog(#"We can now receive QL Events.");
return YES;
}
- (void)beginPreviewPanelControl:(QLPreviewPanel *)panel
{
// This document is now responsible of the preview panel
// It is allowed to set the delegate, data source and refresh panel.
[QLPreviewPanel sharedPreviewPanel].delegate = self;
[QLPreviewPanel sharedPreviewPanel].dataSource = self;
}
- (void)endPreviewPanelControl:(QLPreviewPanel *)panel
{
// This document loses its responsisibility on the preview panel
// Until the next call to -beginPreviewPanelControl: it must not
// change the panel's delegate, data source or refresh it.
return;
}
// Quick Look panel data source
- (NSInteger)numberOfPreviewItemsInPreviewPanel:(QLPreviewPanel *)panel
{
return 1;
}
- (id <QLPreviewItem>)previewPanel:(QLPreviewPanel *)panel previewItemAtIndex:(NSInteger)index
{
int selectedRow = [self selectedRow];
return [NSURL URLWithString:[[[self dataSource] tableView:self objectValueForTableColumn:fileColumn row:selectedRow] stringValue]];
}
// Quick Look panel delegate
- (BOOL)previewPanel:(QLPreviewPanel *)panel handleEvent:(NSEvent *)event
{
// redirect all key down events to the table view
return NO;
}
// This delegate method provides the rect on screen from which the panel will zoom.
- (NSRect)previewPanel:(QLPreviewPanel *)panel sourceFrameOnScreenForPreviewItem:(id <QLPreviewItem>)item
{
NSRect iconRect = [self rectOfColumn:1];
/*
// check that the icon rect is visible on screen
NSRect visibleRect = [self visibleRect];
// convert icon rect to screen coordinates
iconRect = [self convertRectToBase:iconRect];
iconRect.origin = [[self window] convertBaseToScreen:iconRect.origin];
*/
return iconRect;
}
// This delegate method provides a transition image between the table view and the preview panel
- (id)previewPanel:(QLPreviewPanel *)panel transitionImageForPreviewItem:(id <QLPreviewItem>)item contentRect:(NSRect *)contentRect
{
int selectedRow = [self selectedRow];
NSImage *fileIcon = [[NSWorkspace sharedWorkspace] iconForFile:[[[self dataSource] tableView:self objectValueForTableColumn:fileColumn row:selectedRow] stringValue]];
return fileIcon;
}
#end
Thanks for any help.
The documentation isn't the best for this, since it's a new feature that was added in 10.6. (Well, there is obviously the class and protocol references, but in my experience, I've always found the Companion Guides to be more helpful in understanding how the objects are intended to be used in a real-world scenario).
The QLPreviewPanelController Protocol Reference defines 3 methods:
QLPreviewPanelController Protocol Reference
The Quick Look preview panel shows previews for items provided by the first object in the responder chain that implements the methods in this protocol. You typically implement these methods in your window controller or delegate. You should never try to modify preview panel state if you’re not controlling the panel.
- (BOOL)acceptsPreviewPanelControl:(QLPreviewPanel *)panel;
- (BOOL)beginPreviewPanelControl:(QLPreviewPanel *)panel;
- (void)endPreviewPanelControl:(QLPreviewPanel *)panel;
I'm guessing that your code should look like this:
- (BOOL)acceptsPreviewPanelControl:(QLPreviewPanel *)panel
{
return YES;
}
You shouldn't be doing anything in that method besides returning YES. acceptsPreviewPanelControl: is sent to every object in the responder chain until something returns YES. By returning YES, that object effectively becomes "the controller". The latter 2 methods are called on the controller object after it returns YES from the first method. So you should only be setting the delegate and datasource in the beginPreviewPanelControl: method (at which time you will be regarded as the current controller).
- (void)beginPreviewPanelControl:(QLPreviewPanel *)panel
{
// This document is now responsible of the preview panel
// It is allowed to set the delegate, data source and refresh panel.
[QLPreviewPanel sharedPreviewPanel].delegate = self;
[QLPreviewPanel sharedPreviewPanel].dataSource = self;
NSLog(#"We can now receive QL Events.");
}
First:
-acceptsPreviewPanelControl should only return YES and not try to set delegate and datasource.
Then, the problem is that you get an exception breaking the panel:
2010-12-24 15:32:18.291 BackMeUp[4763:80f] -[NSPathStore2 stringValue]: unrecognized selector sent to instance 0x5ecb10
The exception is very likely caused by these invocations:
[[[self dataSource] tableView:self objectValueForTableColumn:fileColumn row:selectedRow] stringValue]
Very likely, [[self dataSource] tableView:self objectValueForTableColumn:fileColumn row:selectedRow] is a file path (a NSPathStore instance which is a subclass of NSString) so it does not respond to -stringValue
So replace:
[NSURL URLWithString:[[[self dataSource] tableView:self objectValueForTableColumn:fileColumn row:selectedRow] stringValue]]
by:
[NSURL fileURLWithPath:[[self dataSource] tableView:self objectValueForTableColumn:fileColumn row:selectedRow]]
Also remove the call to -stringValue in transitionImageForPreviewItem.
As a side note, it seems to be suboptimal to load the image at each call of transitionImageForPreviewItem. I suppose you already have the image computed elsewhere (displayed in your table view), try to use it.

Resources