UIMenuController and UITextView selected text - uitextview

first i am using this UITextView class
textViewClass.h
#interface textViewClass : UITextView
textViewClass.m
#implementation textViewClass
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSMutableArray *items = [[[UIMenuController sharedMenuController] menuItems] mutableCopy];
if (!items) items = [[NSMutableArray alloc] init];
UIMenuItem *menuItem;
menuItem = [[UIMenuItem alloc] initWithTitle:#"Add note" action:#selector(doSomethingHere:)];
[items addObject:menuItem];
[[UIMenuController sharedMenuController] setMenuItems:items];
}
return self;
}
- (void)doSomethingHere:(UITextView *)textView
{
NSLog(#"add note: ");
}
And in MainViewController.h
#import "textViewClass.h"
#interface testViewController : UIViewController <UITextViewDelegate>
#property (strong, nonatomic) IBOutlet textViewClass *myTextView;
And in MainViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[self.myTextView setEditable:NO];
[self.myTextView setDelegate:self];
[self.myTextView setText:#"Lorem ipsum dolor sit er elit lamet,
consectetaur cillium adipisicing pecu,
sed do eiusmod tempor incididunt ut labore
et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco
laboris nisi ut aliquip ex ea commodo consequat."];
}
#pragma mark - UITextView Delegate
- (void)textViewDidChangeSelection:(UITextView *)textView
{
NSString *string = [self.myTextView textInRange:textView.selectedTextRange];
NSLog(#"%#", string);
}
I want to show 'Add note' when i selected some text in myTextView and copy this selection to clipboard and do some action as i want.

hmm you need two corrections. The first is:
you have to implement the UIResponder method like this in your custom UITextView class.
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if (action == #selector(doSomethingHere:) || action == #selector(copy:)) {
if (self.selectedRange.length > 0) {
return YES;
}
}
return NO;
}
here you are filtering the actions with the code which has been defined under the UIResponderStandardEditActions informal protocol.
if (action == #selector(doSomethingHere:) || action == #selector(copy:))
if you only need yourAction only then do
if (action == #selector(doSomethingHere:))
Second correction: if you implement the protocol like this
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
return YES;
}
you will see all possible commands by default therefor: Either filter it by selector or add your own command only like this,
UIMenuItem *menuItem;
menuItem = [[UIMenuItem alloc] initWithTitle:#"Add note" action:#selector(doSomethingHere:)];
[items addObject:menuItem];
[[UIMenuController sharedMenuController] setMenuItems:#[menuItem]];
Hope it will help you...
NB: For more information please check NSHipster

Related

Can not run sample test Koin

I want to run sample test from this site https://insert-koin.io/docs/2.0/getting-started/junit-test/
class HelloAppTest : AutoCloseKoinTest() {
val model by inject<HelloMessageData>()
val service by inject<HelloService>()
#Before
fun before() {
startKoin {
modules(helloModule)
}
}
#Test
fun tesKoinComponents() {
val helloApp = HelloApplication()
helloApp.sayHello()
assertEquals(service, helloApp.helloService)
assertEquals("Hey, ${model.message}", service.hello())
}
}
But it gives following error :
No tests found for given includes: [HelloAppTest]
(filter.includeTestsMatching)
My gradle file is this :
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
buildscript {
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.31")
}
}
plugins {
kotlin("jvm") version "1.3.21" // duplicate for variable
}
group = "adf"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
jcenter()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation("org.koin:koin-core:2.0.0-rc-1")
testCompile("org.koin:koin-test:2.0.0-rc-1")
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
tasks.withType<Test> {
useJUnitPlatform()
}
How to handle it? Anybody, please help me
And this is just for stackoverflow.com:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
you need to include following imports:
testImplementation "junit:junit:4.12"

C++/CX No Suitable Copy Constructor

I have a problem with C++/CX. I'm trying to create a class that is actually a collection of other Class. The class is declared here in the header file:
#pragma once
namespace AdeptlyAdaptiveLayout
{
public ref class NewsItem sealed
{
public:
NewsItem(int init_Id, Platform::String^ init_Category, Platform::String^ init_Headline, Platform::String^ init_Subhead, Platform::String^ init_DateLine, Platform::String^ Image);
property int Id;
property Platform::String^ Category;
property Platform::String^ Headline;
property Platform::String^ Subhead;
property Platform::String^ DateLine;
property Platform::String^ Image;
};
public ref class NewsItemCollection sealed
{
public:
Platform::Collections::Vector<NewsItem> getNewsItems();
};
}
and this is the source file
#include "pch.h"
#include "NewsItem.h"
using namespace Platform;
using namespace Platform::Collections;
namespace AdeptlyAdaptiveLayout
{
NewsItem::NewsItem(int init_Id,
String^ init_Category,
String^ init_Headline,
String^ init_Subhead,
String^ init_DateLine,
String^ init_Image)
{
Id = init_Id;
Category = init_Category;
Headline = init_Headline;
Subhead = init_Subhead;
DateLine = init_DateLine;
Image = init_Image;
}
Vector<NewsItem> NewsItemCollection::getNewsItems()
{
Vector<NewsItem> temp;
temp.Append(*ref new NewsItem(1, "Financial", "Lorem Ipsum", "doro sit amet", "Nunc tristique nec", "Assets/Financial1.png"));
temp.Append(*ref new NewsItem(2, "Financial", "Etiam ac felis viverra", "vulputate nisl ac, aliquet nisi", "tortor porttitor, eu fermentum ante congue", "Assets/Financial2.png"));
temp.Append(*ref new NewsItem(3, "Financial", "Integer sed turpis erat", "Sed quis hendrerit lorem, quis interdum dolor", "in viverra metus facilisis sed", "Assets/Financial3.png"));
temp.Append(*ref new NewsItem(4, "Financial", "Proin sem neque", "aliquet quis ipsum tincidunt", "Integer eleifend", "Assets/Financial4.png" ));
temp.Append(*ref new NewsItem(5, "Financial", "Mauris bibendum non leo vitae tempor", "In nisl tortor, eleifend sed ipsum eget", "Curabitur dictum augue vitae elementum ultrices", "Assets/Financial5.png" ));
temp.Append(*ref new NewsItem(6, "Food", "Lorem ipsum", "dolor sit amet", "Nunc tristique nec", "Assets/Food1.png" ));
temp.Append(*ref new NewsItem(7, "Food", "Etiam ac felis viverra", "vulputate nisl ac, aliquet nisi", "tortor porttitor, eu fermentum ante congue", "Assets/Food2.png" ));
temp.Append(*ref new NewsItem(8,"Food", "Integer sed turpis erat", "Sed quis hendrerit lorem, quis interdum dolor", "in viverra metus facilisis sed","Assets/Food3.png" ));
temp.Append(*ref new NewsItem(9, "Food","Proin sem neque", "aliquet quis ipsum tincidunt", "Integer eleifend", "Assets/Food4.png" ));
temp.Append(*ref new NewsItem(10, "Food", "Mauris bibendum non leo vitae tempor", "In nisl tortor, eleifend sed ipsum eget", "Curabitur dictum augue vitae elementum ultrices", "Assets/Food5.png" ));
return temp;
}
}
The problem is that I keep having this error message "class 'AdeptlyAdaptiveLayout::NewsItem' has no suitable copy constructor". I don't have any idea what I've done wrong. Can you guys me a clue?
Okay, I found the answer now. I've changed the type from Vector to IVector, explicitly create the copy constructor, and adding several changes in the implementation. Here is the changed code in the header and in the source.
Header:
namespace AdeptlyAdaptiveLayout
{
public ref class NewsItem sealed
{
public:
NewsItem(int init_Id, Platform::String^ init_Category, Platform::String^ init_Headline, Platform::String^ init_Subhead, Platform::String^ init_DateLine, Platform::String^ Image);
NewsItem(NewsItem^ obj);
property int Id;
property Platform::String^ Category;
property Platform::String^ Headline;
property Platform::String^ Subhead;
property Platform::String^ DateLine;
property Platform::String^ Image;
};
public ref class NewsItemCollection sealed
{
public:
Windows::Foundation::Collections::IVector<NewsItem^>^ getNewsItems();
};
}
Source:
#include "pch.h"
#include "NewsItem.h"
using namespace Platform;
using namespace Platform::Collections;
using namespace Windows::Foundation::Collections;
namespace AdeptlyAdaptiveLayout
{
NewsItem::NewsItem(int init_Id,
String^ init_Category,
String^ init_Headline,
String^ init_Subhead,
String^ init_DateLine,
String^ init_Image)
{
Id = init_Id;
Category = init_Category;
Headline = init_Headline;
Subhead = init_Subhead;
DateLine = init_DateLine;
Image = init_Image;
}
NewsItem::NewsItem(NewsItem^ obj)
{
this->Category = obj->Category;
this->DateLine = obj->DateLine;
this->Headline = obj->Headline;
this->Id = obj->Id;
this->Image = obj->Image;
this->Subhead = obj->Subhead;
}
IVector<NewsItem^>^ NewsItemCollection::getNewsItems()
{
IVector<NewsItem^>^ temp = ref new Vector<NewsItem^> ();
temp->Append(ref new NewsItem(1, "Financial", "Lorem Ipsum", "doro sit amet", "Nunc tristique nec", "Assets/Financial1.png"));
temp->Append(ref new NewsItem(2, "Financial", "Etiam ac felis viverra", "vulputate nisl ac, aliquet nisi", "tortor porttitor, eu fermentum ante congue", "Assets/Financial2.png"));
temp->Append(ref new NewsItem(3, "Financial", "Integer sed turpis erat", "Sed quis hendrerit lorem, quis interdum dolor", "in viverra metus facilisis sed", "Assets/Financial3.png"));
temp->Append(ref new NewsItem(4, "Financial", "Proin sem neque", "aliquet quis ipsum tincidunt", "Integer eleifend", "Assets/Financial4.png" ));
temp->Append(ref new NewsItem(5, "Financial", "Mauris bibendum non leo vitae tempor", "In nisl tortor, eleifend sed ipsum eget", "Curabitur dictum augue vitae elementum ultrices", "Assets/Financial5.png" ));
temp->Append(ref new NewsItem( 6, "Food", "Lorem ipsum", "dolor sit amet", "Nunc tristique nec", "Assets/Food1.png" ));
temp->Append(ref new NewsItem(7, "Food", "Etiam ac felis viverra", "vulputate nisl ac, aliquet nisi", "tortor porttitor, eu fermentum ante congue", "Assets/Food2.png" ));
temp->Append(ref new NewsItem(8,"Food", "Integer sed turpis erat", "Sed quis hendrerit lorem, quis interdum dolor", "in viverra metus facilisis sed","Assets/Food3.png" ));
temp->Append(ref new NewsItem(9, "Food","Proin sem neque", "aliquet quis ipsum tincidunt", "Integer eleifend", "Assets/Food4.png" ));
temp->Append(ref new NewsItem(10, "Food", "Mauris bibendum non leo vitae tempor", "In nisl tortor, eleifend sed ipsum eget", "Curabitur dictum augue vitae elementum ultrices", "Assets/Food5.png" ));
return temp;
}
}
However, I still do not understand why it works. I think I'm going to do an in-depth research on this.
When you provided the constructor with this constructor signature:
NewsItem(int init_Id, Platform::String ....
... the compiler will no longer synthesize the default ctors for you. You now need to specify them yourself in this case.
https://msdn.microsoft.com/en-us/library/s16xw1a8.aspx
If any non-default constructors are declared, the compiler does not
provide a default constructor

How to display a string with a delay in between words in Swift?

So I've tried using Matt's delay code but it does not seem to fit my purpose. I'm writing a Swift app and would like to be able to display a string (say a word at a time) with a delay in between. Anyone have any suggestions?
This will give you a delay of four seconds between two words. You dont say how many words you need to do this with and without any context or you posting any code its not easy to understand your requirements. However this will provide a delay between two strings
self.mystring.text = "some word"
let seconds = 4.0
let delay = seconds * Double(NSEC_PER_SEC) // nanoseconds per seconds
var dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(dispatchTime, dispatch_get_main_queue(), {
// Performed with delay
self.mystring.text = "a different word"
})
class ViewController: UIViewController {
#IBOutlet weak var myTypeWriter: UITextView!
let words = "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda. Hello World !!!".componentsSeparatedByString(" ")
var counter = 0
var timer:NSTimer?
// create a method to add one word at a time to your text view
func addWord(){
// make sure you only update your text field if you have a corresponding item in your array
if counter < words.count {
myTypeWriter.text = "\(myTypeWriter.text) \(words[counter])"
// invalidate your timer when it is finished
} else {
timer?.invalidate()
}
counter++
}
override func viewDidLoad() {
super.viewDidLoad()
timer = NSTimer.scheduledTimerWithTimeInterval(0.25, target: self, selector: "addWord", userInfo: nil, repeats: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}

navigationItem not showing on popoverController

The navigationbar is failing to appear, works fine in a UITableView, but fails inside a popoverController
Initiate a popover popoverController in UIViewController
-(IBAction) btnShowMovies:(id) sender {
if (self.popoverController == nil) {
teamAController *movies =
[[teamAController alloc]
initWithNibName:#"teamAController"
bundle:[NSBundle mainBundle]];
UIPopoverController *popover =
[[UIPopoverController alloc] initWithContentViewController:movies];
popover.delegate = self;
[movies release];
self.popoverController = popover;
[popover release];
}
CGRect popoverRect = [self.view convertRect:[btn frame]
fromView:[btn superview]];
popoverRect.size.width = MIN(popoverRect.size.width, 100);
[self.popoverController
presentPopoverFromRect:popoverRect
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionDown
animated:YES];
}
teamAController.h
#interface teamAController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
UITableView *tableView;
NSArray *theArray;
}
#property (nonatomic, retain) NSArray *theArray;
#property (nonatomic, retain) IBOutlet UITableView *tableView;
-(void) createArray;
teamAController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title= #"FooBarExtreme";
self.contentSizeForViewInPopover = CGSizeMake(250.0, 300.0);
[self createArray];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
Everything works, I have lovely table with images etc, correct sized and placed popover just no title bar..... ?
I found the solution/problem by following the tutorial at http://mobiforge.com/designing/story/using-popoverview-ipad-app-development.
Worth noting that I found this the most comprehensive one on creating uiPopoverController with uiNavigationBar elements from UIButtons.
The issue is that the popover itself belongs to the view that calls it. The content is derived from the xlib/view you load into it. But not the titlebar. You call that in the parent view view.
This code is in the main view and is called from the UIButton
// BookMarksViewController is the class that contains the code/xib for the popover's content
// Of overarching importance is creating it as a UITableViewController
if (self.popoverController == nil) {
BookMarksViewController *bookMarksViewController =
[[BookMarksViewController alloc]
initWithNibName:#"BookMarksViewController"
bundle:[NSBundle mainBundle]];
// Here's the rub: because in effect this view is controlling the popover
// we have to assign nav bar stuff here. Sigh.
bookMarksViewController.navigationItem.title = #"Territories";
UINavigationController *navController =
[[UINavigationController alloc]
initWithRootViewController:bookMarksViewController];
bookMarksViewController.contentSizeForViewInPopover = CGSizeMake(320, 400);
UIPopoverController *popover =
[[UIPopoverController alloc]
initWithContentViewController:navController];
popover.delegate = self;
[bookMarksViewController release];
[navController release];
self.popoverController = popover;
[popover release];
}
CGRect sourceRect = [self.view convertRect:[btn frame] fromView:[btn superview]];
[self.popoverController presentPopoverFromRect:sourceRect
inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];

click on searchbar terminates my program on Iphones using Objective C

I want to add simple search bar in the table view. Program getting run with no error, but when I try to add text in search bar, program gets terminates
following is my code:
#interface RootViewController : UITableViewController<UISearchBarDelegate> {
NSArray *list;
IBOutlet UISearchBar *searchBar;
NSMutableArray *searchresult;
BOOL isSearchon;
BOOL canSelectRow;
}
#property(nonatomic, retain)NSArray *list;
#property(nonatomic, retain)IBOutlet UISearchBar *searchBar;
-(void)doneSearching:(id)sender;
-(void)searchlist;
#end
//in .m
#implementation RootViewController
#synthesize list,searchBar;
-(void)doneSearching:(id)sender
{
isSearchon=NO;
canSelectRow=YES;
self.tableView.scrollEnabled=YES;
self.navigationItem.rightBarButtonItem=nil;
[searchBar resignFirstResponder];
[self.tableView reloadData];
}
-(void)searchlist
{NSString *searchText = searchBar.text;
[searchresult removeAllObjects];
for(NSString *str in list)
{
NSRange titleResultsRange=[str rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
[searchresult addObject:str];
}
}
- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar {
[self searchlist];
}
- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText {
if([searchText length]>0)
{isSearchon =YES;
canSelectRow=YES;
self.tableView.scrollEnabled=YES;
[self searchlist];}
else{
isSearchon =NO;
canSelectRow=NO;
self.tableView.scrollEnabled=NO;
}
[self.tableView reloadData];
}
- (void)viewDidLoad {
[super viewDidLoad];
list = [[NSArray alloc]initWithObjects:#"rohan",#"vidhya",#"kavita",#"pushkar",nil];
self.tableView.tableHeaderView = searchBar;
searchBar.autocorrectionType=UITextAutocorrectionTypeYes;
searchresult=[[NSMutableArray alloc]init];
isSearchon =NO;
canSelectRow=YES;
self.navigationItem.title = #"Names";
}
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
isSearchon =YES;
canSelectRow=NO;
self.tableView.scrollEnabled=NO;
self.navigationItem.rightBarButtonItem=[[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneSearching:)]autorelease];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(isSearchon){
return [searchresult count];}
else{
return [list count];}
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if(isSearchon)
{
cell.text=[searchresult objectAtIndex:indexPath.row];}
else{
cell.text=[list objectAtIndex:indexPath.row];}
// Configure the cell.
return cell;
}
- (void)dealloc {
[super dealloc];
[searchBar release];
}
#end
I have edited my code in my question...it works fine nw....AnyOne willing to use searchbar in tableview in Iphones can refer the code.

Resources