Window with a view controller [closed] - ios

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Hi guys i am trying to make a window with a view controller (which i guess is like GroupBox in .Net (fix me if i'm wrong..)) and i was trying to start the application on the simulator and it threw an exception. I am using storyboards.
#interface ViewController ()
#end
#implementation ViewController
-(id)init
{
self = [super init];
self.arSongsCollection = [[NSMutableArray alloc] init];
_tableView.delegate = self;
_tableView.dataSource = self.arSongsCollection;
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[_arSongsCollection addObject:[[Song alloc] initWithTitle:#"Song" andArtist:#"Artist" andURL:[NSURL URLWithString:#"http://songurl.com/song.mp3"]]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

Read more about tableview datasources, here's the problem:
_tableView.dataSource = self.arSongsCollection;
this ususally should also point to self
_tableView.dataSource = self;
and you have to implement both necessary tableview delegate and dataSource methods in the view controller class.

Related

Why we are using the method -(id)init and what are the uses?

While referring a sample code i found this snippet can any explain why it is used.
- (id)init
{
self = [super init];
if (self) {
[[self view]setBackgroundColor:[UIColor redColor]];
}
return self;
}
and what is the difference between the following snippet.
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
}
init and viewDidLoad both are completely different.
viewDidLoad called, when the view is loaded into memory, this method called once during the life of the view controller object. It's a great place to do any view initialization.
init method is an initializer method. Cocoa has various types of intializer. To learn more, please check the link,
https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaEncyclopedia/Initialization/Initialization.html

How to create a tip balloon? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Here is my image.I want to create a tip balloon like this. This must visible when user types in textfield
I have created one more but that is in cloud shape. Please help to create this
Subclass the UITextField then override becomeFirstResponder (show bubble) and resignFirstResponder (hide bubble) and when the field is active present your bubble.
.h
#import <UIKit/UIKit.h>
#interface BubbleTextField : UITextField
#end
.m
#import "BubbleTextField.h"
#interface BubbleTextField ()
{
UIView *_bubbleView;
}
- (void)showBubble:(BOOL)show;
#end
#implementation BubbleTextField
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialize the _bubbleView here
}
return self;
}
#pragma mark - Override
- (BOOL)becomeFirstResponder {
BOOL shouldBecome = [super becomeFirstResponder];
if (shouldBecome)
{
[self showBubble:YES];
}
return shouldBecome;
}
- (BOOL)resignFirstResponder {
BOOL shouldResign = [super resignFirstResponder];
if (shouldResign)
{
[self showBubble:NO];
}
return shouldResign;
}
#pragma mark - Private Methods
- (void)showBubble:(BOOL)show {
// Show/Hide
// Animations etc.
}
#end
Check out KBPopupBubble.
And CMPopTipView
Also, check out these search results for custom controls on cocoa controls. Search Results.

What is the recommended way to factor iOS UI initialization? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
In an iOS view controller I typically have code like:
- (void)viewDidLoad
{
UIScrollView *scrollView = [[UIScrollView alloc] init];
// [several lines of code to configure the view]
[self.view addSubview:scrollView]
}
This tends to get cluttered so I add a helper method (e.g. createScrollView) to alloc, init, and configure the view. Is this an established pattern for building views and is there a convention for naming the helper methods? One thing I noticed is that the name initScrollView is not allowed because of ARC.
Here's the pattern I follow:
In init / initWithFrame: create your objects, and set any properties which will never change during the life of this controller:
- (instancetype) init {
self = [super init];
if (self) {
_textField = [UITextField new];
_textField.keyboardType = UIKeyboardTypeEmailAddress;
}
return self;
}
If you want to separate these out into methods like createTextFields, etc., that's fine, although it's easier to debug if you can see a list of everything instantiated in one place.
In viewDidLoad, set up the view hierarchy:
- (void) viewDidLoad {
[self.view addSubview:self.textField];
}
In viewWillLayoutSubviews, set the frames (if you're not using auto-layout):
- (void) viewWillLayoutSubviews {
self.textField.frame = CGRectMake(10, 44, 320, 50);
}
This approach will set you up for success handling view resizing and rotation events.

How do I make a bingo board in xcode? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
In a game that I am developing for iPhone, I would like to create a bingo board in which you can click one of the spots, and the camera opens. I have the camera part down, but I'm working on creating the board. I thought a Collection View with 25 items would work for the grid, but nothing is appearing on the screen when the app is running. Is there a better way to go about making the table?
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:YES animated:animated];
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:NO animated:animated];
[super viewWillDisappear:animated];
}
- (IBAction)cameraButtonClicked:(id)sender {
if (![UIImagePickerController isSourceTypeAvailable:(UIImagePickerControllerSourceTypeCamera)]) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Camera Not Available" message:#"The camera feature isn't available on your device." delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil];
[alertView show];
}else{
//Show the Image Picker Controller Here
UIImagePickerController * ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
ipc.allowsEditing = NO;
//Set the Delegate
ipc.delegate = self;
[self.navigationController presentViewController:ipc animated:YES completion:nil];
}
}
#pragma mark ImagePicker Delegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
self.imageView.image = image;
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
I think your bingo board might be better implemented with multiple views or custom drawing. I personally would opt for custom drawing since I'm pretty comfortable there. Regarding your collection view idea, the only reason it may not be the best is because of less control over the inter-item spacing. In my experience, it is difficult to get the items to sit immediately between each other. I'll go down each potential route and give you my 2 cents:
1. Collection View
Do some research on laying out a perfectly aligned grid. You'll probably have to have a custom layout or change the collection view inset and / or minimumInteritemSpacing. See the following SO posts:
how do you determine spacing between cells in UICollectionView flowLayout
ios UICollectionView separation on top / bottom between cells
Cell spacing in UICollectionView
2. Manually-placed views
This technique will work if you know that you will only have a set number of bingo "slots" or "squares". You could create this in code, a storyboard, or a xib.
3. Custom Drawing / Layout
I would advocate for this technique because of its flexibility. You could pass the number of bingo tiles that you need and then either build UIButtons with a flat tile-like appearance or with the button style UIButtonTypeCustom. Another custom drawing way is to draw with CoreGraphics. However, this would not lend itself well to clean actions like from a UIButton and would require that you re-implement touch methods.
Conclusion
I would either try to go with the collection view if you can get the inter-item spacing worked out or I would go with calculated views laid out in code or if you have a fixed number of items then laid out in a storyboard or something.

CALayer delegation causes zombie crash -- why?

I am new to Core Animation and having trouble implementing a CALayer object with the drawLayer method in a delegate.
I have narrowed the problem down to a very simple test. I have a main viewController named LBViewController that pushes a secondary viewController called Level2ViewController. In the level 2 controller, in viewWillAppear:, I create a CALayer object with it's delegate=self (i.e. the level 2 controller). Whether or not I actually implement the drawLayer:inContext: method I have the same problem -- when I return to the main viewController I get a zombie crash. In the profiler it appears that the object in trouble is the level 2 viewController object -- which is being dealloc'ed after it's popped.
I've tried using a subclassed CALayer object instead of the delegate and it works fine. If I comment out the delegate assignment it also runs fine. I would like to understand why delegation is causing this problem. Any advice is greatly appreciated.
Here's my code ---
Level2ViewController
#implementation Level2ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewDidAppear:animated];
CALayer *box1 = [[CALayer alloc] init];
box1.delegate = self; // problem disappears if I comment out this assignment
box1.backgroundColor = [UIColor redColor].CGColor;
box1.frame = CGRectMake(10,10,200,300);
[self.view.layer addSublayer:box1];
[box1 setNeedsDisplay];
}
// makes no difference whether or not this method is defined as long
// as box1.delegate == self
- (void)drawLayer:(CALayer *)theLayer inContext:(CGContextRef)theContext
{
CGContextSaveGState(theContext);
CGContextSetStrokeColorWithColor(theContext, [UIColor blackColor].CGColor);
CGContextSetLineWidth(theContext, 3);
CGContextAddRect(theContext, CGRectMake(5, 5, 40, 40));
CGContextStrokePath(theContext);
CGContextRestoreGState(theContext);
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
The method in LBViewController (the main controller) that pushes the level 2 view controller
- (IBAction)testAction:(id)sender {
Level2ViewController *controller = [[Level2ViewController alloc]
initWithNibName:#"Level2ViewController" bundle:nil];
controller.title = #"Level2";
// this push statement is where the profiler tells me the messaged zombie has been malloc'ed
[self.navigationController pushViewController:controller animated:YES];
[controller release];
}
You may want to set the layer's delegate to nil before the delegate object is released. So in your Leve2ViewController do this:
-(void)viewWillDisappear:(BOOL)animated
{
if (box1) {
box1.delegate = nil;
}
box1 = nil;
}
Obviously this requires, that box1 is turned into a field (so it is accessible in viewWillDisappear:)
Since you create box1in viewWillAppear: the code above uses viewWillDisappear:. Recently, when I ran into a similar problem, I had a separate delegate object in which I used init and dealloc.
Note: You call [super viewDidAppear:animated]; in viewWillAppear. Looks like a typo or copy/paste glitch :-)

Resources