So I've got a simple UI, A Start button, some text/labels, and a progress bar. When the Start button is click, the NSProgressIndicator should animate. Here's my code:
#synthesize progressBar = progressBar;
/* Some code and setup stuffs... */
- (IBAction)startAction:(id)sender {
NSLog(#"Button clicked.");
NSLog(#"Beginning Socketry and socket creation...");
[progressBar setUsesThreadedAnimation:YES];
[progressBar setIndeterminate:YES];
[progressBar startAnimation:progressBar];
}
I've checked multiple source (Apple Developer Portal, etc.), and nothing works. I don't have to have the NSPIndicator, but it would be really nice if I could.
Also, here's my AppDelegate.h file:
#import <Cocoa/Cocoa.h>
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
#property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
#property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
#property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#property (weak) IBOutlet NSProgressIndicator *progressBar;
- (IBAction)saveAction:(id)sender;
- (IBAction)startAction:(id)sender;
- (IBAction)changeStatus:(id)sender;
- (IBAction)changeProgress:(id)sender;
#end
Go to the Attributes Inspector of the Progress Indicator and set up as shown in the picture below:
See code below:
#synthesize progressBar;
- (IBAction)startAction:(id)sender {
[progressBar setHidden:NO];
[progressBar setIndeterminate:YES];
[progressBar setUsesThreadedAnimation:YES];
[progressBar startAnimation:nil];
}
Related
I am using THEOS, and I know how to hook to methods and override and stuff. But now what I want to do is, I want to access the UI views that are declared within (#interface).
Here is the header file I am referring to:
#class NSString, UILabel, UINavigationBar, UISwitch;
__attribute__((visibility("hidden")))
#interface SettingsViewController : UIViewController <UINavigationBarDelegate, UIBarPositioningDelegate>
{
UINavigationBar *navBar;
UISwitch *alertSwitch;
UISwitch *lightSwitch;
UISwitch *lockSwitch;
UISwitch *infoSwitch;
UILabel *alertLabel;
UILabel *lightLabel;
UILabel *lockLabel;
UILabel *showInfoLabel;
UILabel *infoLabel;
}
#property(retain, nonatomic) UILabel *infoLabel; // #synthesize infoLabel;
#property(retain, nonatomic) UILabel *showInfoLabel; // #synthesize showInfoLabel;
#property(retain, nonatomic) UILabel *lockLabel; // #synthesize lockLabel;
#property(retain, nonatomic) UILabel *lightLabel; // #synthesize lightLabel;
#property(retain, nonatomic) UILabel *alertLabel; // #synthesize alertLabel;
#property(retain, nonatomic) UISwitch *infoSwitch; // #synthesize infoSwitch;
#property(retain, nonatomic) UISwitch *lockSwitch; // #synthesize lockSwitch;
#property(retain, nonatomic) UISwitch *lightSwitch; // #synthesize lightSwitch;
#property(retain, nonatomic) UISwitch *alertSwitch; // #synthesize alertSwitch;
#property(retain, nonatomic) UINavigationBar *navBar; // #synthesize navBar;
- (void).cxx_destruct;
- (void)didReceiveMemoryWarning;
- (void)dismissSettingsViewController;
- (long long)preferredStatusBarStyle;
- (void)showDevInfo;
- (void)infoSwitchSwitched;
- (void)lockSwitchSwitched;
- (void)lightSwitchSwitched;
- (void)alertSwitchSwitched;
- (void)viewDidLoad;
- (long long)positionForBar:(id)arg1;
// Remaining properties
#property(readonly, copy) NSString *debugDescription;
#property(readonly, copy) NSString *description;
#property(readonly) unsigned long long hash;
#property(readonly) Class superclass;
#end
I want to access the UI views under #interface (Ex: UISwitch *alertSwitch;).
How can I access it? Please help I am trying to figure it out since three days :(. Any help or suggestion is very very appreciated.
I hope this help
%hook SettingsViewController
-(void) WhereEverYouWantToRunYourCode {
self.alertSwitch = ....
}
%end
I am just learning and trying to put forth some of the knowledge with objective c and cocoa. I have a little program that has a login screen which then displays a main menu. On the main menu view, there is a button that should load a view to be able to add some data to the app. When I click on the button, it doesn't fire the IBAction but throws an error
EXC_BAD_ACCESS [StartMenuViewController performSelector:withObject:]: message sent to deallocated instance 0x6080195e9f90
I know that it is because there is an object that is not instantiated when the message is being sent, but I cannot find out why. Sorry for the code and the novice level here is the main menu view controller StartMenuViewController.m file:
#import "StartMenuViewController.h"
#import "AppDelegate.h"
#import "MasterViewController.h"
#interface StartMenuViewController ()
#end
#implementation StartMenuViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do view setup here.
}
-(IBAction)showStrainView:(id)sender{
AppDelegate *delegate = nil;
delegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
MasterViewController *addStrainView = [[MasterViewController alloc] initWithNibName:#"MasterViewController" bundle:nil];
[delegate swapView:self.view toView:addStrainView.view];
}
#end
here is the StartMenuViewController.h
#import <Cocoa/Cocoa.h>
#interface StartMenuViewController : NSViewController
#property (weak) IBOutlet NSButton *showStrainViewButton;
#end
Here is the AppDelegate.h
#import <Cocoa/Cocoa.h>
#class loginView, MasterViewController,StartMenuViewController;
#interface AppDelegate : NSObject <NSApplicationDelegate>{
loginView *loginView;
MasterViewController *masterViewController;
}
#property (assign) IBOutlet NSWindow *window;
#property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
#property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
#property (strong) loginView *loginView;
#property (strong) MasterViewController *masterViewController;
#property (strong) StartMenuViewController *starMenuViewController;
-(void)swapView:(NSView *)view1 toView:(NSView *)view2;
#end
Here is the AppDelegate.m
#import "AppDelegate.h"
#include "MasterViewController.h"
#import "ScaryBugDoc.h"
#import "Strains.h"
#import "loginView.h"
#interface AppDelegate()
#end
#implementation AppDelegate
#synthesize managedObjectContext = _managedObjectContext;
#synthesize managedObjectModel = _managedObjectModel;
#synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
#synthesize loginView;
#synthesize masterViewController;
#synthesize starMenuViewController;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// 1. Create the master View Controller
self.loginView = [[loginView alloc] initWithNibName:#"loginView" bundle:nil];
// 2. Add the view controller to the Window's content view
[self.window.contentView addSubview:self.loginView.view];
self.loginView.view.frame = ((NSView*)self.window.contentView).bounds;
}
-(void)swapView:(NSView *)view1 toView:(NSView *)view2{
//[view1 removeFromSuperview];
[self.window.contentView addSubview:view2];
view2.frame = ((NSView*)self.window.contentView).bounds;
}
#end
If you need the loginView.h and loginView.m files I can add them as well. I would appreciate any and all help. Thanks in advance
The problem is here:
MasterViewController *addStrainView = [[MasterViewController alloc]initWithNibName:#"MasterViewController" bundle:nil];
[delegate swapView:self.view toView:addStrainView.view];
You don't keep a reference to the MasterViewController so it gets deallocated even though its view is kept around. Now anytime the view tries to access methods from the view controller, things fail.
You should make addStrainView a child view controller of self in the above code so the view controller sticks around as long as its view.
I'm trying to create an app that has three UISlider used for selecting R, G and B values. The slider should change the colour of a label according to the sliders values.
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
NSInteger color;
IBOutlet UISlider *redSlider;
IBOutlet UISlider *greenSlider;
IBOutlet UISlider *blueSlider;
IBOutlet UILabel *colorLabel;
}
#property (nonatomic, retain) UISlider *redSlider;
#property (nonatomic, retain) UISlider *greenSlider;
#property (nonatomic, retain) UISlider *blueSlider;
colorLabel.textColor = [UIColor colorWithRed: redSlider.value green:greenSlider.value
blue:blueSlider.value alpha:1];
#end
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.
}
#synthesize redSlider, greenSlider, blueSlider;
#end
This is what I have so far and I'm totally stuck.
You can do this like...
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
// you need to make sure these are actually created
// and give them all a target of self and action sliderValueChanged
#property (nonatomic, retain) UISlider *redSlider;
#property (nonatomic, retain) UISlider *greenSlider;
#property (nonatomic, retain) UISlider *blueSlider;
// not sure where this is set up
#property (nonatomic, strong) UILabel *colorLabel;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// You may have to create your labels and sliders in here...
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)sliderValueChanged
{
// the sliders have changed so set the colour of the label
// based on the new values of the sliders.
self.colorLabel.textColor = [UIColor colorWithRed:self.redSlider.value
green:self.greenSlider.value
blue:self.blueSlider.value
alpha:1.0];
}
#end
Because you don't actually have a question I'm going to take a guess that your code doesn't compile, because it wouldn't.
colorLabel.textColor = [UIColor colorWithRed: redSlider.value green:greenSlider.value blue:blueSlider.value alpha:1];
The above line doesn't belong in you interface. I would recommend you change the code you currently have to something like.
ViewController.h
#interface ViewController : UIViewController
// You don't need the ivars anymore these get generated with the properties below
#property (nonatomic, retain) IBOutlet UISlider *redSlider;
#property (nonatomic, retain) IBOutlet UISlider *greenSlider;
#property (nonatomic, retain) IBOutlet UISlider *blueSlider;
#property (nonatomic, retain) IBOutlet UILabel *colorLabel
#property (assign) NSInteger color;
#end
ViewController.m
#implementation ViewController
// Also no need for the synthesize as these also get generated automatically
- (void)viewDidLoad
{
[super viewDidLoad];
// To change the actual color when changing the value of a slider move this to a method that gets called on slider changed.
colorLabel.textColor = [UIColor colorWithRed:self.redSlider.value green:self.greenSlider.value self.blue:blueSlider.value alpha:1];
}
#end
And once you actually ask a question I might also be able to help you with that.
Your ViewController.h :-
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
IBOutlet UISlider *redSlider;
IBOutlet UISlider *greenSlider;
IBOutlet UISlider *blueSlider;
IBOutlet UILabel *colorLabel;
}
#property (nonatomic, retain) UISlider *redSlider;
#property (nonatomic, retain) UISlider *greenSlider;
#property (nonatomic, retain) UISlider *blueSlider;
#end
and your ViewController.m will be like this:-
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize redSlider, greenSlider, blueSlider;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self changeLabelColor];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(Void) changeLabelColor
{
colorLabel.backgroundcolor = [UIColor colorWithRed:redSlider.value green:redSlider.value blue:redSlider.value alpha:1.0];
}
#end
As i didn’t copied paste and i have written it, there might be some typo..
textcolor wouldn’t change label color so i changed it to backgroundcolor.
you can also add slider method with IBAction.
I fix all warnings and now I have such error in my AppDelegate Class
.h file is
#import <UIKit/UIKit.h>
#interface UYLAppDelegate : UIResponder <UIApplicationDelegate>{
}
#property (retain, nonatomic) IBOutlet UIWindow *window;
#property (retain, nonatomic) IBOutlet UISplitViewController *splitViewController;
#end
.m file is
#import "UYLAppDelegate.h"
#import "UYLMasterViewController.h"
#import "UYLDetailViewController.h"
#import <QuartzCore/QuartzCore.h>
#implementation UYLAppDelegate
#synthesize window = _window;
#synthesize splitViewController = _splitViewController;
- (void)dealloc
{
[_window release];
[_splitViewController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIView *rootView = [[self.splitViewController.viewControllers objectAtIndex:0] view];
rootView.layer.borderWidth = 1.0f;
rootView.layer.cornerRadius =5.0f;
rootView.layer.shadowOpacity = 0.8f;
rootView.layer.shadowOffset = CGSizeMake(-5, 0);
self.window.rootViewController = self.splitViewController; //error here
[self.window makeKeyAndVisible];
return YES;
}
#end
in this row I take an error
self.window.rootViewController = self.splitViewController; //error here
SplitViewController consist of 2 controllers, .h file are:
#import <UIKit/UIKit.h>
#class UYLDetailViewController;
#interface UYLMasterViewController : UITableViewController
#property (retain, nonatomic) IBOutlet UYLDetailViewController *detailViewController;
#end
and the second
#import <UIKit/UIKit.h>
#import "UYLModalViewController.h"
#class UYLMasterViewController;
#interface UYLDetailViewController : UIViewController <UYLModalViewControllerDelegate>{
UIBarButtonItem *_MessageButton;
NSInteger modalViewShowType;
}
-(IBAction)buttonTapped:(id)sender;
#property (retain, nonatomic, getter=_MessageButton) IBOutlet UIBarButtonItem *someMessageButton;
#property (retain, nonatomic) NSNumber *detailItem;
#property (retain, nonatomic) IBOutlet UIToolbar *toolbar;
#property (retain, nonatomic) IBOutlet UILabel *detailTitle;
#property (retain, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
#property (assign, nonatomic) BOOL masterIsVisible;
#end
edit:
there is all I see in console
GNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Mon Aug 15 16:03:10 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".sharedlibrary apply-load-rules all
Attaching to process 8217.
Pending breakpoint 1 - ""UYLDetailViewController.m":117" resolved
Pending breakpoint 2 - ""UYLModalViewController.m":30" resolved
Pending breakpoint 3 - ""UYLModalViewController.m":41" resolved
Pending breakpoint 4 - ""UYLDetailViewController.m":63" resolved
Pending breakpoint 5 - ""UYLMasterViewController.m":46" resolved
Pending breakpoint 7 - ""UYLDetailViewController.m":36" resolved
No breakpoint number 7.
Current language: auto; currently objective-c
Please help me to understand it =(
I had some SIGABRT errors most of the time, it just means that it can't find that instance you are referring to. So look at that line again and think what might not be declared right.
I think it might help if you change it to.
self.window.rootViewController = splitViewController;
I am working on an app using multiple view controllers and the navigation controller. When the application runs and executes the following code, it throws an exception when trying to add the sub view.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window addSubview:[navigationController view]];
[self.window makeKeyAndVisible];
}
I declare the navigation controller like so:
#interface SimpleContactsAppDelegate : NSObject <UIApplicationDelegate> {
NSManagedObjectModel *managedObjectModel;
NSManagedObjectContext *managedObjectContext;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
UIWindow *window;
UINavigationController *navigationController;
// view for adding new contacts
UIViewController *newContactView;
UIButton *addButton;
// controls for the addContactView
UIButton *saveContactButton;
UITextField *nameField;
UITextField *emailField;
UITextField *phoneField;
UITableView *contactsTable;
}
#property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
#property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (nonatomic, retain) IBOutlet UIButton *addButton;
#property (nonatomic, retain) IBOutlet UITableView *contactsTable;
// controller and fields for the form
#property (nonatomic, retain) IBOutlet UIViewController *newContactView;
#property (nonatomic, retain) IBOutlet UITextField *nameField;
#property (nonatomic, retain) IBOutlet UITextField *emailField;
#property (nonatomic, retain) IBOutlet UITextField *phoneField;
#property (nonatomic, retain) IBOutlet UIButton *saveContactButton;
I have connected the navigation controller to the Delegate object in the XIB. Feel free to have a look at my full source: https://github.com/agmcleod/SimpleContacts/tree/parttwo
I've tried using Instruments with NSZombie, but it doesnt seem to stop and let me inspect what has particularly gone wrong. It also just keeps running sometimes, and won't terminate. I end up having to force quit it using active terminal.
First of all, you declare a property, but access the UINavigationController through its instance variable instead of using the property.
Use this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window addSubview:[self.navigationController view]];
[self.window makeKeyAndVisible];
return YES;
}
Second, you changed the name of your main nib file to "Window.xib". You either have to change it back to "MainWindow.xib" or you will have to edit your SimpleContacts-Info.plist and change the value for "Main nib file base name" to "Window".