I am currently trying to write some automated tests within instruments and am having a bit of an issue, is it possible to retrieve the number of views/panes in a scrollView so that I can say, if more than one view, scrollRight() for example?
int count = scrollview.subviews.count;
if ( count > 1 )
{
.....
}
??
EDIT:
You will need to get a reference to the view you are looking for. This will be done by iterating through your windows subview collection. In objective-C it would be something like this:
NSInteger count = 0;
for ( UIView *view in self.window.subviews )
{
if([view isKindOfClass:[UIScrollView class]])
{
count = (UIScrollView *) view.subviews.count;
}
}
Related
I'm writing a number of objects to NSPasteboard for a drag operation using beginDraggingSessionWithItems:event:source::
NSMutableArray *draggingItems = [NSMutableArray array];
for (NSUInteger i = 0; i < numItems; i++) {
NSPasteboardItem *pasteboardItem = [NSPasteboardItem new];
[pasteboardItem setDataProvider:[MyItem itemForIndex:i]
forTypes:#[#"com.test.FooType"]];
NSDraggingItem *draggingItem = [[NSDraggingItem alloc] initWithPasteboardWriter:pasteboardItem];
// ...
[draggingItems addObject:draggingItem];
}
[sourceView beginDraggingSessionWithItems:draggingItems
event:theEvent
source:self];
And in the dragging destination (in the same app), I try to receive these items using readObjectsForClasses:options:, but I only receive one of them.
- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender
{
// sender.draggingPasteboard.pasteboardItems contains all items
NSArray *myItems = [sender.draggingPasteboard readObjectsForClasses:#[[MyItem class]] options:nil];
// pasteboard:item:provideDataForType: is called for each object on the pasteboard...
// ...but this method only returns 1 object (the first one)??
(Note that if you pass #[[NSPasteboardItem class]] for the classes, you get all items back unmodified.)
Any idea why this would this happen? Sample project available here (with lots of NSLogs to see that the items are actually being written to the pasteboard).
Turns out the implementation of -pasteboard:item:provideDataForType: needs to set data on the item, not on the pasteboard — doing the latter overwrites previously written data for other items.
i created a Single View Application in XCode..
My Integer shows up strange values..
I declared the integer score with the value of 0 in the viewDidLoad method and
if two UIImageViews gets hitting each other the value of score gets every time 10 points bigger..
Game.h
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
int score;
#interface Game : UIViewController{
Game.m:
//score
score = 0;
Score.text = [NSString stringWithFormat:#"Score: %i", score];
Collision:
if(CGRectIntersectsRect(BulletFive.frame, Asteroid.frame)){
BulletFive.hidden = NO;
[self Score];
[self PlaceAsteroid];
Score:
-(void)Score{
Score.text = [NSString stringWithFormat:#"Score: %i", score];
score = score + 10;
if (score > HighScore) {
[[NSUserDefaults standardUserDefaults] setInteger:score forKey:#"HighScoreSaved"];
}
}
My problem:
If I click on a store button in the main menu and go to the game again, it shows up very strange values like 16060 or 26110 in a UILabel. The storebutton has nothing to do with the other view controller. What could be wrong or what is wrong with XCode ?
Sorry for my english, Google Translator isn´t very helpful..
when you declare the int or NSInteger make sure, it's not a pointer.
Wrong :
int *variable1;
NSInteger *variable1;
Right one:
int variable1;
NSInteger variable1;
Please add more description, so I can help you further.
How do you specify a group when initially displaying an ABPeoplePickerNavigationController (so it doesn't automatically display "All Contacts")?
Yeah, I do. I had to make it work.
Set your class as the delegate of the people picker (pp.delegate = self;)
Then implement:
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if([navigationController.viewControllers count] > 1) {
navigationController.delegate = nil;
[navigationController popViewControllerAnimated:NO];
}
}
It seems to work best with animation off, but still works with it on but sort of goofy. Only tested on simulator.
D
I have 2 arrays. One is a collection of buttons. The other one should be an array of dictionaries. I am logging the length/size/count of object in the arrays in the console using [arrayListName count]:
2012-07-19 19:56:59.001 ABC[3224:707] lastest_badge_outlet_collection count: 4
2012-07-19 19:56:59.007 ABC[3224:707] badges_array count: 1
ok so when running this loop I want to populate the images with the key value 'name' from each of the dictionaries in existence in the array (that is badges_array). At the moment I have dictionary stored in that array (which is fine). However when I run this through the loop it always populates the third image along
for(int i = 0; i < [lastest_badge_outlet_collection count]; i++){
UIButton *button = [lastest_badge_outlet_collection objectAtIndex:i];
if(i < [badges_array count]){
NSDictionary *badge_d = [badges_array objectAtIndex:i];
NSString *badge_d_image_string = [badge_d objectForKey:#"image"];
UIImage *badge_d_image = [UIImage imageNamed: badge_d_image_string];
[[button imageView] setImage:badge_d_image];
[[button imageView] setContentMode: UIViewContentModeScaleAspectFit];
[button setAlpha:1.0f];
} else {
[button setAlpha:0.5f];
}
}
Theoretically it should be populating the first image. Why is it doing this? How does it decide which order the items are in the outlet collection for example...
Mind boggled.
I tried rewiring them to the collection 1 by 1 by wiring them up in order with no success....
Here is a screenshot.
Thanks!
I don't think there is a way to predict the order of IBOutletCollection.
I guess what you need to do is sort the collection before use it.
You can set tag property for each button and sort them by those number.
Check this answer for more detail.
Can you fast enumerate a NSIndexSet? if not, what's the best way to enumerate the items in the set?
In OS X 10.6+ and iOS SDK 4.0+, you can use the -enumerateIndexesUsingBlock: message:
NSIndexSet *idxSet = ...
[idxSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
//... do something with idx
// *stop = YES; to stop iteration early
}];
A while loop should do the trick. It increments the index after you use the previous index.
/*int (as commented, unreliable across different platforms)*/
NSUInteger currentIndex = [someIndexSet firstIndex];
while (currentIndex != NSNotFound)
{
//use the currentIndex
//increment
currentIndex = [someIndexSet indexGreaterThanIndex: currentIndex];
}
Fast enumeration must yield objects; since an NSIndexSet contains scalar numbers (NSUIntegers), not objects, no, you cannot fast-enumerate an index set.
Hypothetically, it could box them up into NSNumbers, but then it wouldn't be very fast.
Short answer: no. NSIndexSet does not conform to the <NSFastEnumeration> protocol.
Supposing you have an NSTableView instance (let's call it *tableView), you can delete multiple selected rows from the datasource (uhm.. *myMutableArrayDataSource), using:
[myMutableArrayDataSource removeObjectsAtIndexes:[tableView selectedRowIndexes]];
[tableView selectedRowIndexes] returns an NSIndexSet.
No need to start enumerating over the indexes in the NSIndexSet yourself.
These answers are no longer true for IndexSet in Swift 5. You can perfectly get something like:
let selectedRows:IndexSet = table.selectedRowIndexes
and then enumerate the indices like this:
for index in selectedRows {
// your code here.
}